Magento 2: Getting unknown category
-
04-03-2021 - |
Question
I am trying to get a level 2 category list. I got all level 2 categories but I am also getting 2 unknown categories which are not in backend. This is weird. Here is my code
public function getShopCategories()
{
$objectManagerr = \Magento\Framework\App\ObjectManager::getInstance();
$categoryFactory = $objectManagerr->create('Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');
$categories = $categoryFactory->create()
->addAttributeToSelect('*')
->addAttributeToFilter('is_active', 1) //only active categories
->addAttributeToFilter('include_in_menu', 1)
->addAttributeToFilter('level', array('eq'=>2));
return $categories;
}
I reindexed and checked for different store views but not getting these categories.
La solution
Check unknown category is present in the database? You can use below query to getcategories.
SELECT e.*, IF(at_name.value_id > 0, at_name.value, at_name_default.value) AS name FROM catalog_category_entity AS e INNER JOIN catalog_category_entity_varchar AS at_name_default ON (at_name_default.entity_id = e.entity_id) AND (at_name_default.attribute_id = '41') LEFT JOIN catalog_category_entity_varchar AS at_name ON (at_name.entity_id = e.entity_id) AND (at_name.attribute_id = '41')
I hope it helps!