How to add "order by" on a Magento Query

How to add "order by" on a Magento Query

August 14, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to add "order by" on a Magento Query

Sorting Magento Order Comments by “created_at”

Are you facing issues sorting your order comments correctly within your Magento store? It’s a common scenario when custom scripts are involved. Let me guide you through the process of setting an “order by” clause for your comment grid.

Understanding the Current Issue

The provided code snippet joins the sales/order_status_history table with your main order table using a LEFT JOIN on the parent_id column. However, it currently lacks sorting capability based on any specific field like created_at. The resulting query groups by the main entity ID but doesn’t sort the output in any particular sequence.

Adding Sorting to Your Query

To sort your comments by the “created at” time (created_at field), you need to add an ORDER BY clause to your query. Here’s how you can modify your _initSelect() method:

protected function _initSelect()
{
    parent::_initSelect();

    // Join order comment
    $this->getSelect()->joinLeft(
        array('ordercomment_table' => $this->getTable('sales/order_status_history')),
        'main_table.entity_id = ordercomment_table.parent_id AND ordercomment_table.comment IS NOT NULL',
        array(
            'ordercomment' => 'ordercomment_table.comment',
        )
    )->group('main_table.entity_id')
    ->order(array('ordercomment_table.created_at DESC'));

    return $this;
}

What the Code Does

In this updated code:

  1. The joinLeft() method remains unchanged, linking your order table with the comments table based on the parent ID relationship.
  2. The new line ->group('main_table.entity_id') ensures that each main entity (order) is represented only once in the result set.
  3. Finally, we add a new line to sort the joined results by the created_at field of the comments table in descending order (DESC).

Sorting Order

By using DESC, you are sorting your order comments in reverse chronological order—meaning the most recent comment will appear first for each order.

Remember, it’s crucial to ensure that your custom scripts are properly integrated with Magento’s core functionalities and any patches or updates you apply don’t override this customization. If you’re looking for more advanced techniques or need to optimize your database queries further, consider reaching out to our expert team at Laravel Company.

Final Thoughts

Sorting order comments is a fundamental aspect of maintaining a well-organized and user-friendly Magento storefront. By following this approach, you can ensure that customers and administrators alike have easy access to the most recent comments related to each order.

Don’t hesitate to explore other customization options or seek professional help for more complex scenarios on our platform at Laravel Company. We’re always ready to assist with your Magento development needs.

Database Admin Configuration Orders

Our network