Magento: paginate filtered product collection

Magento: paginate filtered product collection

July 15, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento: paginate filtered product collection

Magento: Paginate Filtered Product Collection

As a senior developer, I understand your frustration when working with filtered product collections in Magento. You’ve set up your filters correctly but are struggling to implement pagination that actually works. Let’s dive into the issue and find a solution.

The Problem with Your Current Approach

Your current code is on the right track, but there’s one critical mistake causing the entire collection to be returned instead of just three items per page:

// pagination (THIS DOESNT WORK!)
$product_collection->setPageSize(3)->setCurPage(1);

The setPageSize and setCurPage methods are not enough to properly paginate a collection in Magento. You need to call the load method after setting these properties for pagination to take effect.

The Correct Way to Paginate Filtered Collections

To fix this, you should modify your code as follows:

// fetch all visible products
$product_collection = Mage::getModel('catalog/product')->getCollection();

// set wanted fields (necessary for filter)
$product_collection->addAttributeToSelect('name');
$product_collection->addAttributeToSelect('description');
$product_collection->addAttributeToSelect('price');
$product_collection->addAttributeToFilter('visibility', array('neq' => 1));

// filter by name or description
$product_collection->addFieldToFilter(array(
      array('attribute'=>'name','like'=>$sTerm),
      array('attribute'=>'description','like'=>$sTerm)
));

// filter for max price
foreach ($product_collection as $key => $item) {
    if($item->getPrice() >= $priceTo){
        $product_collection->removeItemByKey($key);
    }
}

// set page size and current page
$product_collection->setPageSize(3)->setCurPage(1);

// load the collection to apply pagination (this is the crucial step)
$product_collection->load();

Notice that I’ve added the load method call at the end. This tells Magento to actually execute the query with the specified page size and current page.

Best Practices for Paginating Collections

When working with collections in Magento, here are a few best practices to keep in mind:

  1. Always load the collection after setting pagination properties: Without the load method, pagination won’t work as expected.

  2. Use the correct attribute names: Ensure you’re using the right attribute names for your filters and selected attributes.

  3. Manage memory efficiently: If working with large collections, consider using incremental loading or limiting the collection size to avoid running out of memory.

  4. Cache your results (if possible): If your filters don’t change frequently, consider caching the filtered product collection to improve performance.

Conclusion

In this blog post, we’ve identified and resolved a common issue encountered when paginating filtered product collections in Magento. By properly setting page size, current page, and ensuring that the load method is called after configuring pagination properties, you can now effectively limit your results per page.

Remember to adjust the code as needed for your specific use case, and don’t hesitate to reach out if you have any further questions or need additional guidance on Magento development. For more technical resources and best practices, feel free to explore our comprehensive blog at https://laravelcompany.com.

Catalog Configuration Pricing Errors

Our network