{"id":3669,"date":"2024-10-28T07:30:00","date_gmt":"2024-10-28T07:30:00","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/how-to-use-setpagesize-in-magento-2-collections\/"},"modified":"2025-01-02T10:02:44","modified_gmt":"2025-01-02T10:02:44","slug":"how-to-use-setpagesize-in-magento-2-collections","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/how-to-use-setpagesize-in-magento-2-collections\/","title":{"rendered":"How to Use setPageSize in Magento 2 Collections?"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In case you want to <a href=\"https:\/\/meetanshi.com\/blog\/add-pagination-in-magento-2-custom-collection\/\">add pagination in a custom collection<\/a>, you may require to size of each page. That&#8217;s where you can use the setPageSize method in Magento 2.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this quick guide, you&#8217;ll learn what is setPageSize method and how you can use it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is setPageSize in Magento 2?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The setPageSize method in Magento 2 is used to control the number of items (like products, customers, or orders) displayed on a page in your online store. This is helpful to limit the page size when you&#8217;re creating pagination for a custom collection.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In addition to pagination, the setPageSize is also used to limit the number of items in admin grids, product listings, and search results.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The setPageSize Syntax<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Typically, the method is used with the collection object as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$collection-&gt;setPageSize($pageSize);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In the above syntax, the setPageSize method assigns the maximum number of records for each page in the collection object. Here, we have assigned the $pageSize variable. However, you can also pass static integer values.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, the following code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$collection-&gt;setPageSize(10);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">will limit the maximum number of records per collection to 10.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That&#8217;s how easily you can use setPageSize in Magento 2 to limit the number of items per collection. I hope you&#8217;ll find this tutorial helpful. In case you have any further queries or doubts, feel free to leave a comment.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In case you want to add pagination in a custom collection, you may require to size of each page. That&#8217;s where you can use the&#8230;<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-3669","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/3669","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=3669"}],"version-history":[{"count":1,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/3669\/revisions"}],"predecessor-version":[{"id":5599,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/3669\/revisions\/5599"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=3669"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=3669"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=3669"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}