Magento: Load a Product by an Attribute

Today I'm discussing about the Load a product by an attribute filter. At this point we assume that you have set up a new attribute, added it to the appropriate attribute set and set the desired values for the appropriate products.

In this example the attribute identifier is color.

    // Instantiate a product collection object
    $products = Mage::getResourceModel('catalog/product_collection');
    // Select which fields to load into the product
    // * will load all fields but it is possible to pass an array of
    // select fields to load
    // Ensure the product is visible
    $products->addAttributeToFilter('visibility', array('neq' => 1));
    // Ensure the product is enabled
    $products->addAttributeToFilter('status', 1);
    // Add Color filter
    $products->addAttributeToFilter('color', 'green');
    // Limit the collection to 1 result
    // Load the collection
    if ($products->getFirstItem()) {
        $product = $products->getFirstItem();
        echo $product->getName();
    else {
        echo 'No products found ' . $name;