Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3272986
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:53:04+00:00 2026-05-17T18:53:04+00:00

I have developed a custom search engine for our Magento store and I am

  • 0

I have developed a custom search engine for our Magento store and I am trying to load the product collection in a very specific order (I have ranked the results according to an algorithm I designed).

I can load the product collection correctly, however it is not in the order that I would like it to be in. Here is basically how it is working now:

My database query basically comes back with a PHP array of product IDs. For this example lets say it looks like this:

$entity_ids = array(140452, 38601 );

Now I can transpose the 140452 and the 38601 and the product collection comes back in the same order each time. I would like the product collection to be in the same order as the ID of the entity ids.

The code I am using to create my collection is as follows:

$products = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('entity_id', array('in' => $entity_ids))
    ->setPageSize($results_per_page)
    ->setCurPage($current_page)
    ->load();

Is there a way to set the sort order to be the order of the $entity_ids array?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T18:53:04+00:00Added an answer on May 17, 2026 at 6:53 pm

    Collections inherit from the class

    Varien_Data_Collection_Db
    

    There’s a method named addOrder on that class.

    public function addOrder($field, $direction = self::SORT_ORDER_DESC)
    {
        return $this->_setOrder($field, $direction);
    }
    

    So, you’d think something like this should work for basic ordering

    Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('*')
    ->addOrder('entity_id');
    

    However, it doesn’t. Because of the complex joining involved in EAV Collections, there’s a special method used to add an attribute to the order clause

    Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection::addAttributeToSort
    

    However again, this can only be used to add simple attributes. To create an arbitrary sort, you’ll need to manipulate the Zend_Select object directly. I’m not a big fan of this, and I’m not a big fan of using custom mysql functions to achieve things, but it appears it’s the only way to do this

    I tested the following code on a stock install and got the desired results. You should be able to use it to get what you want.

            $ids = array(16,18,17,19);
            $products = Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToSelect('*')
            ->addAttributeToFilter('entity_id',$ids);           
    
                        //shakes fist at PDO's array parameter
                        $ids = array_map('intval', $ids);
            $products->getSelect()->order("find_in_set(e.entity_id,'".implode(',',$ids)."')");
            foreach($products as $product)
            {
                var_dump($product->getEntityId());
                var_dump($product->getSku());
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have our own custom calendar in our portal which is developed in Java.
I have developed a very simple app that uses a custom adapter for a
I have developed some custom DAO-like classes to meet some very specialized requirements for
I have developed a new custom extension for magento admin section. Main menu option
I have developed a custom dijit Templated Widget. I have to do some DOM
I have developed a custom target, that needs to run only if the compilation
I have developed an SSIS custom task component. It uses a connection manager of
I have developed a PyroCMS custom module, but now I want to add WYSIWYG
I have a working custom search suggestions class (via http://developer.android.com/guide/topics/search/adding-custom-suggestions.html ). It currently returns
Ok, so maybe my search syntax is wrong here, this could have very well

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.