WordPress.org

Ready to get started?Download WordPress

Forums

problem with query_posts and post__in (9 posts)

  1. meep_meep
    Member
    Posted 5 years ago #

    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 ?

  2. nagypali
    Member
    Posted 5 years ago #

    basically, i have the same problem with query_posts.
    using wp 2.6.3

    np

  3. Eladi
    Member
    Posted 5 years ago #

    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?

  4. picchilosa
    Member
    Posted 5 years ago #

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

  5. greggo
    Member
    Posted 4 years ago #

    any resolution here?

  6. prw
    Member
    Posted 4 years ago #

    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'

    so...

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

  7. nublooo
    Member
    Posted 4 years ago #

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

  8. xfactor2000
    Member
    Posted 4 years ago #

    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):
    query_posts("cat=$myCat");

    And this doesn't
    query_posts("cat=$myCat&p!=$post->ID");

    This doesn't work either:
    query_posts("cat=$myCat&p=-$post->ID");

    Any solutions?

  9. MichaelH
    Member
    Posted 4 years ago #

    <?php
    //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];
        $args=array(
          'cat' => $first_cat, //cat__not_in wouldn't work
          'post__not_in' => array($post->ID),
          'showposts'=>5,
          'caller_get_posts'=>1
        );
        $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>
           <?php
          endwhile;
        } //if ($my_query)
      } //if ($cats)
      wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic