Q:

How to get the CategoryId using the name in Magento 1.9?

Looking to find the category ID using the Name attribute programmatically. My script returns a 1 when it should be a 19. Here is my code:
$collection = Mage::getModel('catalog/category')->getCollection()
    ->addAttributeToFilter('is_active', 1) 
    ->addAttributeToFilter('parent_id', $currentCategoryId) 
    ->addAttributeToFilter('name', $categoryName);

which should return the category right? Then the script utilizes $catid = $collection->getFirstItem()->getId(); to get the ID, however it is returning the wrong ID. Any ideas?

A:

$category = Mage::getResourceModel('catalog/category_collection')
    ->addFieldToFilter('name', 'Men')
    ->getFirstItem() // The parent category
        ->getChildrenCategories()
        ->addFieldToFilter('name', 'Clothing')
        ->getFirstItem(); // The child category

$categoryId = $category->getId();

A:

Please try using the following.
$_category = Mage::getResourceModel('catalog/category_collection')
        ->addFieldToFilter('name', $categoryName)
        ->getFirstItem();

$categoryId = $_category->getId();

