Sort Query by Post In

Query for a specific set of post IDs, and return them in that same order.

NOTE: This plug-in has been incorporated into WordPress core as of version 3.5.

Sort Query by Post In is a very light weight (less than 10 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 in the post__in parameter.

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

Jake Goldman
Jake Goldman
  • Rating
  • Requires 3.0+
  • Downloads 2954
  • Updated 2012-10-20

4 Comments

  1. Rodrigo

    Hi,
    I just installed your plugin, but something looks strange,
    it only worked when I changed
    $thequery->query[…
    to
    $thequery->query_vars[…
    in the plugin code

    maybe something changed in the recent wordpress versions

    ·
    • Jake Goldman
      Jake Goldman

      Quite possibly! I’ll investigate.

      ·
  2. Chella

    Hey, it’s working great… Thanks for a nice plugin…

    ·
  3. John Blackbourn

    I previously wrote some similar functionality using MySQL’s FIELD() function instead of FIND_IN_SET(). I wonder if there’s a performance difference between the two.

    ·

We believe in web publishing made easy. Maybe even fun.

hire us apply