How to add OR condition in Magento collection for created_at and updated_at date?

How to add OR condition in Magento collection for created_at and updated_at date?

September 24, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to add OR condition in Magento collection for created_at and updated_at date?

Improving Magento Product Collections: Adding an OR Condition for Created_at and Updated_at Dates

As a Magento developer, you often need to retrieve products based on specific criteria, including their creation or modification dates. However, the native Magento collection methods don’t directly support adding an OR condition between attributes like created_at and updated_at. In this post, we’ll explore how to achieve this by leveraging Magento’s flexible query builder.

The current code provided is a typical example of loading product collections with specific attributes:

$collection = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('sku')
    // ... other attribute selections ...
    ->addAttributeToFilter('type_id', array('in' => array('simple')))
    ->addAttributeToFilter('updated_at', array('from' => date("Y-m-d", time()-86400)));
    ->addAttributeToFilter('created_at', array('from' => date("Y-m-d", time()-86400)));

As you can see, the code is currently using two separate addAttributeToFilter methods to filter products created or updated within the last 24 hours. To add an OR condition between these filters, we’ll need to use Magento’s addFieldToFilter method instead.

Combining Filters with an OR Condition

The key to achieving this lies in using the addFieldToFilter method with a custom array structure that defines the logical condition:

$collection->getSelect()->where('created_at >= ? OR updated_at >= ?', date("Y-m-d", time()-86400));

Here, we’re directly modifying the select query using the getSelect() method. The where clause uses a conditional operator (>=) to check if either the created_at or updated_at attribute is within the last 24 hours.

This approach effectively combines the two filters into one, applying an OR condition between them. It allows you to retrieve products that have been created or updated within the specified time frame using a single query.

Best Practices and Caveats

While this solution works for most cases, it’s essential to consider some best practices:

  1. Performance: Directly modifying the select query can potentially lead to slower performance if not done carefully. Make sure your filters are as specific as possible to reduce the overall number of rows processed.

  2. Indexing: Ensure that both created_at and updated_at attributes have proper indexing on the database level for optimal performance when using such queries.

  3. Query Complexity: While this approach is convenient, it’s crucial not to overuse direct select query modifications. Magento’s built-in collection methods are optimized for various scenarios, so you should leverage them whenever possible.

Conclusion

In this post, we demonstrated how to add an OR condition between created_at and updated_at filters in a Magento product collection by directly manipulating the underlying select query. This technique allows you to retrieve products based on either of these conditions with a single query, improving your code’s efficiency.

Remember that while this approach is powerful, it’s essential to follow best practices for performance and indexing to ensure optimal results. For more advanced collection manipulation techniques and Magento development insights, be sure to check out Laravel Company for expert resources and tutorials tailored for developers like you.

Catalog Database

Our network