Support » Fixing WordPress » problem with query_posts and post__in

  • Hello,
    I’m having some trouble using query_posts() with post__in.
    here is a simple example :
    Let says I have only 5 posts in my blog and I want them in this order on my homepage : 3, 4, 1, 2 and 5.
    From the codex, if I want to return posts ordered my way, I should use :

    <?php query_posts(array('post__in'=> array(3,4,1,2,5))); ?>
    <?php while (have_posts()) : the_post(); ?>

    but nothing happens, my homepage just return my 5 posts without taking care of my special order (they are ordered by date as default).

    Then I tried this to check if query_posts was actually doing something on posts fetched for homepage, and it does work actually :

    <?php query_posts('p=5'); ?>
    <?php while (have_posts()) : the_post(); ?>

    So is there any problem with post__in or am I missing something here ?

Viewing 8 replies - 1 through 8 (of 8 total)
  • basically, i have the same problem with query_posts.
    using wp 2.6.3


    UP. The same problem.

    I need to display certain posts on my page.
    I used function query_posts() with post__in, Like this:

    query_posts(array('post__in' => array (208, 232, 159, 341)));
    if (have_posts()) : while (have_posts()) : the_post();

    But it doesn’t work. The result is 10 recent posts.
    Can someone help? Are there any WordPress experts?

    same problem…
    i retrieve a certain number of post ids and then i pass them to another query, but it doesn’t work.

    any resolution here?



    i just found the solution (for the second time cause i remember now that i found it before also)..

    you have to add post_type before ‘post__in’


    $query_array = array('post_type' => 'page', 'post__in' => array(1,2,3));



    prw, what are you talking about? Maybe you’d like to re-read the question?



    Hey there,
    I’m having a very similar problem. Basically, I wanted to retrieve posts from current post’s category and those that don’t match current post’s ID.

    This works($myCat is current post’s category):

    And this doesn’t

    This doesn’t work either:

    Any solutions?



    //show 5 posts from the current post's first category,
    //but don't include the current post.
      $cats = wp_get_post_categories($post->ID);
        if ($cats) {
        $first_cat = $cats[0];
          'cat' => $first_cat, //cat__not_in wouldn't work
          'post__not_in' => array($post->ID),
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          echo 'Related Posts';
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        } //if ($my_query)
      } //if ($cats)
      wp_reset_query();  // Restore global post data stomped by the_post().
Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘problem with query_posts and post__in’ is closed to new replies.