I have a custom view but now I want to put the nodes created by a specific user role on top.

I want to do this using code so I can easily implement this throughout multiple web pages.

I'm thinking to alter the query or to put an array sort before the nodes are rendered.

But I don't know which hook would be useful for this.

edit: using hook_views_pre_render as suggested by Jimajamma

function vacancies_views_pre_render(&$view) {
  $view->result = array_reverse($view->result);

But that doesn't change the order of the nodes in the view ( putting a dsm($view); in the if statement ensures me that it is this view that is called. )


You can use View's prerender hook to modify the view's results anyway you want.

For example,

function YOURMODULE_views_pre_render(&$view) {

   if ($view->name=='xyzzy') {

      // modify/sort the $view->result array to your heart's content here
      // for example, reverse it...



Basically $view->result is an array of row results, so, just go through it and grab the rows you want first, then follow that up with the remaining rows.


I created a quick view and did the above reversing and it certainly reversed the order so I am not sure what you are doing ever so slightly wrong to not see similar behavior on your site.

  • Helpful, but changing the $view->result order doesn't change the order that is output on the screen. ( while removing items does remove nodes from the view ) but no matter what items I remove the order doesn't change. – FLY Apr 19 '13 at 12:26
  • see code addition above...not sure what you are doing differently – Jimajamma Apr 19 '13 at 19:45
  • 2
    Changing the result order in hook_views_pre_render() definitely works. – rooby Apr 21 '13 at 12:23
  • Thanks for testing jimajamma and rooby for confirming that this works. Must be something else that's wrong in my installation. But for the question asked this is the answer so I'll accept it. I'll post an update if I find what was wrong. – FLY Apr 22 '13 at 7:50
  • feel free to post your code -- these tiny little errors are sometimes right there and all it takes is a second person to look and see them. – Jimajamma Apr 22 '13 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.