Support » Fixing WordPress » ‘post__not_in’ variable for wp_query does not work..

  • This is on WordPress 2.8.

    I cannot exclude posts using an array of post id’s because post__not_in does not work. I tried looking through the wp-includes/query.php to try and fix it on my own with no luck. Anyone been able to clean this up?

Viewing 15 replies - 1 through 15 (of 28 total)
  • Shameless bump, would really like some help finding a workaround. Pretty much this is what I am doing: Creating two loops (one to output one sticky post at the top of the home page to span two columns, one to show the other 8 entries (2 per row, 4 rows)) to separate sticky post from article’s loop. This way, the second page isn’t short a story or showing duplicates.

    post__not_in works fine for me.

    You might need to put all your code in a pastebin and report the link back here and maybe someone can spot the problem.

    $args_fp = array(
      'post__not_in' => 343,
      'posts_per_page' => 8
    );
    $my_query_fp = new WP_Query($args_fp);
    
    while ($my_query_fp->have_posts()) : $my_query_fp->the_post(); update_post_caches($posts); $preview = get_post_meta($post->ID, 'preview', true); $count++;
    
    echo "post id: ".$post-ID."<br />";
    
    endwhile;

    It does not work. Trust me. 343 is the post ID for the sticky post I do not want to be queried. It comes up in the results of the while loop.

    That does not work either. I have tried everything. (By the way, post 343 is a sticky post) Assigned a variable, directly placing the array, etc, nothing works, the article still shows up in the query which leads me to believe that the ‘post__not_in’ parameter is NOT working correctly.

    Other people are also having this issue:
    http://osdir.com/ml/wp-trac/2010-04/msg02482.html

    I’m sorry I forgot to add that the posts I am trying to not include in the query are sticky posts, not just regular posts.

    I have a sticky post, post ID = 170 and this gets all posts but that post.

    <?php
    $args=array(
      'post__not_in'=> array(170),
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo 'List of 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;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    To clarify usage:
    post__not_in should be fed an array of values(even for just a single value).

    Has worked for me since 2.7, in a number of different scenarios.

    Still doesn’t work. I tried your exact code MichaelH and I still got my sticky post showing up. I even tried it with non sticky posts, still showing. Here is my code:

    $args_fp = array(
    	'post_type' => 'post',
      	'post_status' => 'publish',
      	'posts_per_page' => -1,
    	'post__not_in' => array(343, 493, 166),
      	'caller_get_posts'=> 1
    );
    $fp_query = null;
    $fp_query = new WP_Query($args_fp); 
    
    while ($fp_query->have_posts()) : $fp_query->the_post();
    	update_post_caches($posts); $preview = get_post_meta($post->ID, 'preview', true); $count++;
    
    ?>
    <br />[<b>ARTICLE:</b> <?=$post->ID;?> <?php the_title(); ?>]<br />
    <?
    endwhile;

    So it’s just me that has this problem I’m assuming? 🙁

    Just a wild guess – have you tried it without update_post_caches?

    @vtxyzzy – Just tried it. Same result. This is so frustrating I want to pull my hair out!

    I’d like to see what the query request actually is. Can you add this:

    echo "<p>REQUEST:$wp_query->request</p>";

    right after this:

    $fp_query = new WP_Query($args_fp);

    and post the output?

    @vtxyzzy – Here you go, it should be posts 343 and 206 that are excluded but it looks like it’s excluding some other posts.

    REQUEST: SELECT SQL_CALC_FOUND_ROWS wp_16_posts.* FROM wp_16_posts WHERE 1=1 AND wp_16_posts.ID NOT IN (261,209) AND wp_16_posts.ID NOT IN ( SELECT tr.object_id FROM wp_16_term_relationships AS tr INNER JOIN wp_16_term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy = ‘category’ AND tt.term_id IN (‘4’, ‘0’, ‘0’) ) AND wp_16_posts.post_type = ‘post’ AND (wp_16_posts.post_status = ‘publish’) ORDER BY wp_16_posts.post_date DESC LIMIT 0, 8

    It looks like you have another query active, or some filters changing the query. This query is excluding category id 4, and has an offset of 8. Neither of these is in the parameters you have shown.

    See if you can find the query or filter that specifies these paramters.

Viewing 15 replies - 1 through 15 (of 28 total)
  • The topic ‘‘post__not_in’ variable for wp_query does not work..’ is closed to new replies.