Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Posts in order by id array

By: vtxyzzy on Jun 2nd, 2012  |  syntax: PHP  |  size: 0.51 KB  |  views: 181  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // Show posts in order of post__in array
  2. $id_array = array(589,39,981,531);
  3. $args = array(
  4.    'post__in' => $id_array,
  5.    'ignore_sticky_posts' => 1,  // Stickies will mess this up
  6. );
  7. query_posts($args);
  8. if (have_posts()) :
  9.    usort($wp_query->posts, function($a, $b) use ($id_array) {
  10.       return array_search($a->ID, $id_array) - array_search($b->ID, $id_array);
  11.    });
  12.    while (have_posts()) :
  13.       // Put your own loop code here
  14.       the_post();
  15.       echo "<p>ID: $post->ID</p>";
  16.    endwhile;
  17. endif;