WordPress query_posts() with an array?

I’m trying to query posts based on a number of ID’s that are contained in an array.

My array (called $my_array) looks like this:

Read More
Array
(
    [0] => 108
    [1] => 129
    [2] => 145
)

And my Query looks like this:

<?php query_posts(array('post__in' => $my_array)); ?>

However this just returns one post, the post has the ID of the first item in the array (108).

Do I have my syntax wrong?

Related posts

Leave a Reply

3 comments

  1. $args = array(
      'post_type' => 'page',//or whatever type
      'post__in' => array(108,129,145)
      );
    query_posts($args);
    

    or

    $arr=array(108,129,145);
    $args = array(
      'post_type' => 'page',
      'post__in' => $arr
      );
    query_posts($args);
    
  2. You always have to set the post_type with the post__in argument. So your line should look like the following:

    <?php query_posts(array('post_type' => 'post', 'post__in' => $my_array)); ?>
    

    That will query the posts with the IDs you have in the array.

  3. Daniel,
    I am posting an answer, although you probably found it. I don’t have reputation yet to post comments, query_posts supports all the arguments from WP_Query including ordering you can add ‘orderby’ => ‘title’, ‘order’ => ‘ASC’ to the query_posts call