Sort Query by Post In  for

Sort Query by Post In is a very simple WordPress plug-in that allows post queries to sort the results (loop) in the order specified by the “post__in” parameter.

Description

Sort Query by Post In is a very light weight (about 5 lines of code!) plug-in intended for developers executing custom post queries. You’re welcome to include it in your theme and redistribute — just offer us some credit, please!

When constructing a WordPress post query in your theme template files or plug-in, WordPress offers the option to explicitly specify the posts to retrieve using the “post__in” parameter. Unfortunately, the “orderby” parameter does not offer an option that will sort the result by the exact order passed in the post__in parameter. This plug-in adds a “post__in” option for the “orderby” parameter that will order the result by the exact order specified by the the “post__in” parameter.

And don’t worry about the plug-in being deactivated – your post queries with “post__in” value used for the “orderby” will simply gracefully fall back to the default date sorting.

So what’s the use case? Well, many special themes allow the editor to specify the posts they want to show in certain parts of the front end theme. For example, the user may be able to specific “featured post” #1, #2, and #3 for a part of the home page. Without this plug-in, the template would have to execute a distinct post query for each post to ensure they are presented in the order specified as #1, 2, and 3. Now, a developer can execute a query for all the posts in one shot, sorting them by the specified order.

How does it work?

Let’s say you want to retrieve posts 20, 10, and 106, in that order. You can retrieve those posts without this plug-in using the “post__in” parameter with the post query.

[php]query_posts( array( ‘post__in’ => array(20,10,106)  ) );[/php]

But they’ll be ordered by date, or one of the others you specify with the “orderby” parameter. This plug-in adds a new “orderby” option that will order the posts by the order they were specified in the “post__in” array! Here’s how to modify the query above to return the posts in the order specified:

[php]query_posts( array( ‘post__in’ => array(20,10,106),  ‘orderby’ => ‘post__in’ ) );[/php]

As always, feedback and suggestions are welcome!

6 Thoughts Contributed to “Sort Query by Post In”

  1. BinaryBit - August 7, 2010 at 5:37 pm Reply

    Would you please provide me with a simple example through which a custom post type I created can utilize your code to sort posts in the ADMIN post list by a custom field. In my use case I developed a custom post type for upcoming events and I need to be able to sort the posts in the admin area by my custom field “event_start_date”. Your help is greatly appreciate.

    • Oomph
      Oomph - August 14, 2010 at 2:42 pm

      @BinaryBit – That’s not what this plug-in does.

      You *can* do what you’re striving to do by hooking the edit screen, checking the post type, and then filtering the orderby in the post query. If you have a budget and would like us to actually do it for you, please use the request quote button.

  2. Scott Cariss - October 26, 2010 at 4:31 am Reply

    This bit of code is a life saver. I have some requirements to do some very complex SQL queries to return some posts. Although I can use $wpdb to do the queries and return all the posts that match my query but doing this I loose paging.

    Just returning the ID’s from my query and passing to WP_query is great and I get back the functionality I lost above, but then I would loose the ordring of my posts. This code you have show just solves the final step to the nightmare I’ve been having.

    Thank you so much.

    Scott

  3. Juan Manuel Lopez Salaberry - March 21, 2011 at 6:21 pm Reply

    I’m trying to find the way in which I could allow users to see my posts in a different sorting (random i.e.) but still maintain the latest entry as the first one to be shown..could I set that up with this plugin? thanks for any help!

  4. Tim Carr - September 28, 2011 at 11:12 am Reply

    Seems sorting by post__in still isn’t part of the WordPress core, although it’s flagged as an issue at http://core.trac.wordpress.org/ticket/13729

    Just came across this whilst trying to sort posts by the post__in array.

    Thanks for such a useful plugin.

  5. Ian Atkins - October 10, 2011 at 3:14 pm Reply

    Just stumbled across a possible complete dead end, and you’ve just saved the day. Perfect, thanks a lot.

What do you think?

(required)

Oomph is a full-service digital agency providing strategy, design & development and a host of other web services. A leader in WordPress and Drupal implementation, Oomph pushes the boundaries of today’s web platforms. Oomph has a diverse portfolio of non-profits, international corporations and publications. Team Oomph is always thinking creatively about the digital world. Oomph is located in Providence and Boston.