i want to search product in magento by name and category id

i want to search product in magento by name and category id

September 18, 2026 · Illustrated author avatarBy Stefan Bogdanescu
i want to search product in magento by name and category id

Custom Product Search by Name and Category ID in Magento

Searching products in Magento by name using a ‘LIKE’ query in combination with filtering by category ID can be challenging if not done correctly. In this post, we’ll walk through the process of creating an effective custom search to achieve your desired results.

The Basics: Understanding Your Current Approach

Your current code comes close to achieving the desired result but has a few issues:

$searchstring='comp';
$product_collection = Mage::getResourceModel('catalog/product_collection')
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('category_id', array('in' => array('finset' => '141')));

    ->addAttributeToFilter('name', array('like' => '%'.$searchstring.'%'))
    ->load();

The primary problems are:

  1. The finset value in the in filter for category_id. This is not a valid parameter.

  2. The use of load() at the end of the collection setup. This can lead to memory issues if you’re dealing with large datasets.

Solving the Category Filter Issue

To filter products by category ID, you need to use the correct syntax:

->addAttributeToFilter('category_id', array('in' => array(141)))

In this code snippet, replace 141 with your desired category ID. The 'in' operator is used to match against one or more values.

Improving Performance: Avoiding the Load Method

The load() method can be resource-intensive if you have a large product catalog. It fetches all data for all products in memory right away.

A better approach is to use Magento’s pagination and only load the required number of items at once:

->setCurPage($page)
->setPageSize($limit);

Here, $page represents the current page and $limit represents the number of items per page. This ensures you’re only loading a manageable amount of data.

The Complete Working Example

Combining these improvements, your final code should look something like this:

$searchstring = 'comp';
$categoryId = 141;
$page = 1; // Set the current page number
$limit = 20; // Set the items per page

$product_collection = Mage::getResourceModel('catalog/product_collection');
$product_collection->addAttributeToSelect('*')
    ->addAttributeToFilter('name', array('like' => '%'.$searchstring.'%'))
    ->addAttributeToFilter('category_id', array('in' => array($categoryId)))
    ->setCurPage($page)
    ->setPageSize($limit);

foreach ($product_collection as $product) {
    echo $product->getName().'<br>';
}

Conclusion

Searching products by name and filtering by category ID in Magento requires a careful approach to avoid performance issues. By using the correct filters, avoiding unnecessary data loading, and implementing proper pagination, you can create an efficient custom search solution.

Remember, optimizing your search logic is crucial for maintaining a smooth user experience on your store. Regularly reviewing and tuning your database queries can help ensure your e-commerce platform remains fast and responsive even as your catalog grows.

For more advanced Magento development topics like this one, be sure to check out the Magento Development section on our blog. We cover a wide range of best practices and problem-solving techniques for various e-commerce scenarios.

Catalog Database Performance Pricing

Our network