1

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:

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?

1
  • note that you can use post_type => 'any' if you have a range of ids over multiple post types Commented Nov 25, 2011 at 12:15

3 Answers 3

5
$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);
Sign up to request clarification or add additional context in comments.

1 Comment

is it possible to re-order the posts or are we stuck with the order of the array (which seem to be the case with what I'm working on)
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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.