Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I need to list posts in a very specific order that has nothing to do with the date of the posts or the title, or anything WordPress can figure out for me. I need this order to be used when I list posts in a category and on the category archive page itself.

Is it possible to do something like this, were $idlist was an array of post ids in the order I want, and it could loop through each one?

  <?php query_posts(post_id=$idlist); ?>
  <?php the_permalink(); ?> etc...
share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

First off, query_posts is terrible doesn't use it. It does all kinds of fun things like mess up the very useful conditional tags like is_singular and the like as well as mess up pagination.

Short of hooking into the posts_orderby filter and writing some custom order by SQL, your best bet is to use the post__in argument combined with the post__in orderby argument that was added in WP 3.5.

Example:

<?php
$custom_query = new WP_Query(array(
    'post__in'  => array(1, 2, 4, 5, /* whatever your post ID's are here */),
    'orderby'   => 'post__in',
));

while ($custom_query->have_posts()) {
    $custom_query->the_post();
    // use the normal template tags here
    ?>
    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
    <?php
}

// restore the original query
wp_reset_query();
share|improve this answer
    
Perfect. I put my final code in a gist for my own benefit and in case anyone is interested. –  JPollock Mar 3 '13 at 21:32
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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