Sort view before print I have some node IDs I'm passing to a view; however the sort order is wrong.

Here's my pseudo code:

//query to grab the nids

if (!empty($nodeids)) {
  $view = 'taxo_listing';
  foreach ($nodeids as $nid) {
    print views_embed_view($view, block_1, $nid);
  }
}


What I need to do is pre-build the view then run implement hook_views_query_alter to sort the view before printing out the view.

Been looking at http://views.doc.logrus.com/ for a while now, but it's all a bit confusing, can anyone offer any clues as how to go about this? I know what I want to do but unsure how to go about it, sound familiar guys?

Thanks in advance.
I don't know where $nodeids comes from, but I expect it's an array of nids from a database query? If so, you're effectively running a view with a view and can probably get away without writing any code for hooks.

Therefore, you can either:


Sort the $nodeids yourself with uasort() which will mean probably returning more fields to sort with and maybe writing your own comparison function.
Let Views handle all of this, thus gaining access to the UI and all it's powers.


The latter 'view within a view' approach is better for sustainability and sanity because it uses the Views UI and theme layers more consistently. A good approach that's worked for me is:


Make a view that can either:

return the properly sorted $nodeids, or;
get it to take an argument of a comma-separated list of nids you can pass implode(',', $nodeids) to.

use Views PHP to add a field that can contain your views_embed_view($view, block_1, $nid), probably with $nid replaced by $row-&gt;nid but will depend on your setup.
Add any sorting or sorting code to the view you want.