{"id":547,"date":"2019-08-09T18:23:18","date_gmt":"2019-08-09T18:23:18","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2019\/08\/09\/get-product-collection-by-category-id-in-magento-2\/"},"modified":"2025-05-22T16:11:26","modified_gmt":"2025-05-22T10:41:26","slug":"get-product-collection-by-category-id-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/get-product-collection-by-category-id-in-magento-2\/","title":{"rendered":"How to Get Product Collection by Category ID in  Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">You don\u2019t know how the clients\u2019 requirements can shoot out of nowhere. As a developer, you need to keep your tricks handy to satisfy the clients\u2019 demands.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here\u2019s one such trick to <em><strong>get product collection by category ID in Magento 2<\/strong><\/em>. The solution given in the post lets you get the product collection with details like product name, price, etc. using the category ID.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I have used the below solution in my store to create a category of freebies, i.e., <a href=\"https:\/\/meetanshi.com\/free-magento-extensions.html\" target=\"_blank\" rel=\"noreferrer noopener\">Free Magento Extensions<\/a>!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can use it to create a special category to feature a lifestyle or brand that is a curated collection of products from different categories. Or, filter products based on its type from the category.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Get Product Collection by Category ID in Magento 2:<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$objectManager =  \\Magento\\Framework\\App\\ObjectManager::getInstance();\n$categoryFactory = $objectManager->get('\\Magento\\Catalog\\Model\\CategoryFactory');\n\n$categoryId = 4; \/\/ YOUR CATEGORY ID\n$category = $categoryFactory->create()->load($categoryId);\n\n$categoryProducts = $category->getProductCollection()\n    ->addAttributeToSelect('*');\n\nforeach ($categoryProducts as $product) {\n    \/\/ get Product data\n    print_r($product->getData());\n\n    echo $product->getName();\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can also <a href=\"https:\/\/meetanshi.com\/blog\/programmatically-set-product-position-in-a-category-in-magento-2\/\">set product position in a category in Magento 2 programmatically<\/a> as the product position value determines the order of products on the category page.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I\u2019d be very grateful if you helped share this helpful post on social media to fellow developers!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You don\u2019t know how the clients\u2019 requirements can shoot out of nowhere. As a developer, you need to keep your tricks handy to satisfy the&#8230;<\/p>\n","protected":false},"author":5,"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-547","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/547","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\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=547"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/547\/revisions"}],"predecessor-version":[{"id":15315,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/547\/revisions\/15315"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=547"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=547"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=547"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}