SHARE
TWEET

Posts in order by id array

vtxyzzy Jun 2nd, 2012 316 Never
  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;
RAW Paste Data
Top