WordPress.org

Ready to get started?Download WordPress

Forums

'post__not_in' variable for wp_query does not work.. (29 posts)

  1. virtuexru
    Member
    Posted 4 years ago #

    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?

  2. virtuexru
    Member
    Posted 4 years ago #

    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.

  3. MichaelH
    Member
    Posted 4 years ago #

    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.

  4. virtuexru
    Member
    Posted 4 years ago #

    $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.

  5. MichaelH
    Member
    Posted 4 years ago #

  6. virtuexru
    Member
    Posted 4 years ago #

    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

  7. virtuexru
    Member
    Posted 4 years ago #

    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.

  8. MichaelH
    Member
    Posted 4 years ago #

    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().
    ?>
  9. Mark / t31os
    Moderator
    Posted 4 years ago #

    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.

  10. virtuexru
    Member
    Posted 4 years ago #

    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;
  11. virtuexru
    Member
    Posted 4 years ago #

    So it's just me that has this problem I'm assuming? :(

  12. vtxyzzy
    Member
    Posted 4 years ago #

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

  13. virtuexru
    Member
    Posted 4 years ago #

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

  14. vtxyzzy
    Member
    Posted 4 years ago #

    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?

  15. virtuexru
    Member
    Posted 4 years ago #

    @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

  16. vtxyzzy
    Member
    Posted 4 years ago #

    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.

  17. virtuexru
    Member
    Posted 4 years ago #

    Well my current query is this:

    $args_fp = array(
    	'post_type' => 'post',
      	'post_status' => 'publish',
      	'posts_per_page' => 8,
    	'post__not_in' => array(343, 206),
      	'caller_get_posts'=> 1
    );
    $fp_query = null;
    $fp_query = new WP_Query($args_fp);
  18. vtxyzzy
    Member
    Posted 4 years ago #

    Still, no category 4 exclusion, and the wrong post ID exclusion. Gotta be a filter or another query somewhere.

  19. virtuexru
    Member
    Posted 4 years ago #

    I have another query on the page but it's just to get 1 sticky post:

    global $post;
    $sticky = get_option('sticky_posts');
    $args   = array(
    	'posts_per_page' => 1,
    	'post__in'  => $sticky,
    	'caller_get_posts' => 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
  20. vtxyzzy
    Member
    Posted 4 years ago #

    OK. Just as an experiment, remove any filters just ahead of the creation of $fp_query and print out the query request after.

    $args_fp = array(
    	'post_type' => 'post',
      	'post_status' => 'publish',
      	'posts_per_page' => 8,
    	'post__not_in' => array(343, 206),
      	'caller_get_posts'=> 1
    );
    remove_all_filters('posts_where');
    remove_all_filters('posts_join');
    $fp_query = null;
    $fp_query = new WP_Query($args_fp);
    echo "<p>REQUEST:$wp_query->request</p>";
  21. virtuexru
    Member
    Posted 4 years ago #

    Looks almost identical:

    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

  22. vtxyzzy
    Member
    Posted 4 years ago #

    I may be brain dead. Instead of echoing $wp_query->request, please try $fp_query->request.

  23. virtuexru
    Member
    Posted 4 years ago #

    @vtxyzzy - That's what I did, echoing out wp_query gave nothing so I used fp_query instead.

  24. vtxyzzy
    Member
    Posted 4 years ago #

    OK. Something is stepping on your query. You will have to search for code that excludes category id 4, whatever category that is. Also, see what is unique about posts 261 and 208. Maybe that can help find the other query.

    The only other suggestion I can think of is to try using $xx_query as the name instead of $fp_query. If that gives the same $xx_query->request, it has to be a filter, maybe in a plugin.

  25. virtuexru
    Member
    Posted 4 years ago #

    @vtxyzzy - If it's a filter in some other plugin I'm going to have to look through about 45 plugins :\. Is there any way to just make sure the query is being started from scratch? wp_reset_query?

  26. vtxyzzy
    Member
    Posted 4 years ago #

    If it really is a filter, then the remove_all_filters code I suggested earlier should have taken care of it. I am at a loss to explain it. Did using $xx_query instead of $fp_query make any difference?

    And yes, wp_reset_query is worth a try.

  27. Mark / t31os
    Moderator
    Posted 4 years ago #

    Depends what the filter is on, the above code will only remove filters on two hooks.

    pre_get_posts would be another possible place for a filter/action.

    You could try this to see if there's anything hooked onto pre_get_posts.

    $f = ( isset( $wp_filter['pre_get_posts'] ) ) ? $wp_filter['pre_get_posts'] : 'nothing';
    print '<pre>';
    print_r( $f );
    print '</pre>';

    If you see the nothing text, there's no filter/action on pre_get_posts.

    NOTE: parse_query would be another hook to check.

  28. vtxyzzy
    Member
    Posted 4 years ago #

    The code below will print all filters that contain keywords in their name:

    <?php
    global $wp_filter;
    $keywords = array('post','query');  // Keywords to find
    echo '<p>FILTERS:';
    foreach (array_keys($wp_filter) as $key) {
      foreach ($keywords as $keyword) {
        if (false !== strpos($key,$keyword)) {
          $val = $wp_filter[$key];
          echo "<p>KEY: $key<br /> "; print_r($val);echo '</p>';
        }
      }
    }
    echo '</p>';
    ?>
  29. manlioma
    Member
    Posted 3 years ago #

    Hello ! can anyone help me whit "post_not_in" array ??

    I would to exclude the current category , but not the parent of current.

    this is the example:

    ?php $categories = get_the_category($post->ID);
    
      if ($categories) { $category_ids = array();   
    
    foreach($categories as $individual_category)
    
    $category_ids[] = $individual_category->term_id;
    
        $args=array(
    
            'category__in' =>  $category_ids,
    
    	'post__not_in' => array($post->ID),
    
            'showposts'=>5, // Corresponds to Number of related posts to be shown.
    
            'caller_get_posts'=>1
    
        );
    
     $my_query = new wp_query($args);
    
    if( $my_query->have_posts() ) {
    
    echo '<h2 class="related_post_title">OTHER VIDEOS FROM </h2><ul class="related_post">'; 
    
    while ($my_query->have_posts()) {
    
    $my_query->the_post();?> 
    
    <li><a href="<?php the_permalink() ?>"
    
    rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"> <img src="<?php echo get_post_meta($post->ID, "video_thumbnail", true); ?>" alt="<?php the_title(); ?>" /><?php the_title(); ?></a></li><?php }
    
    echo '</ul>'; } } $post = $backup; wp_reset_query(); ?>

    this is work right...but gets me the relative of category parent and not only the child!

    please help meee :)

Topic Closed

This topic has been closed to new replies.

About this Topic