Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How can I exclude one specific post from a WP_Query query? (For example, show all posts apart from a post with the ID 278)

I've tried the post__not_in argument but it just removes all posts..

Any help would be great.

Here is my current query

<?php
    $temp = $wp_query;
    $wp_query= null;
    $wp_query = new WP_Query(array(
        'post_type' => 'case-study',
        'paged' => $paged,
    ));
    while ($wp_query->have_posts()) : $wp_query->the_post();
?>

Thanks

share|improve this question
add comment

3 Answers

up vote 0 down vote accepted

I just tested the below code, and it works:

            <?php

                $q1 = new WP_query(array('category_name' => 'test-cat-1', 'order' => 'ASC'));

                if($q1->have_posts()) :
                $firstPosts = array();
                    while($q1->have_posts()) : $q1->the_post();
                    $firstPosts[] = $post->ID;
            ?>      

                    <div class="item">

                        <h2><?php the_title(); ?></h2>

                    </div>

            <?php       
                    endwhile;
                endif;

 /*****************************************************************************/

                $q2 = new WP_query(array('post__not_in' => $firstPosts, 'order' => 'ASC' ));

                if($q2->have_posts()) :
                    while($q2->have_posts()) : $q2->the_post();
            ?>      

                    <div class="item">

                        <h2 class="item-title"><?php the_title(); ?></h2>

                    </div>

            <?php       
                    endwhile;
                endif;
            ?>

The first loop displays all posts in a category, and collects the post id's into an array.

The second loop displays all posts, excluding posts from the first loop.

share|improve this answer
    
Perfect, thank you! –  Dean Elliott Sep 15 '12 at 20:38
    
On another note, Is there a way to add wp-pagenavi to the 2nd query? –  Dean Elliott Sep 15 '12 at 22:21
add comment

You have to define the posts__not_in arg as array. Even for a single value. And please don't overwrite global core variables with temporary stuff.

<?php
    $wpse65146_custom_query = new WP_Query(array(
        'post_type' => 'case-study',
        'paged' => $paged,
        'posts__not_in' => array( 1 )
    ) );
    if ( $wpse65146_custom_query->have_posts() ) :
    while ( $wpse65146_custom_query->have_posts() ) :
        $wpse65146_custom_query->the_post();

        // do stuff

    endwhile;
    endif;
?>
share|improve this answer
    
That doesn't seem to have had any effect, I used 43 as an example (One of those posts has this ID) 'posts__not_in' => array( 43 ) Am I doing something wrong? –  Dean Elliott Sep 14 '12 at 15:15
    
Please use exactly what I wrote. Not only that argument. –  kaiser Sep 14 '12 at 15:17
    
Nope, still nothing... –  Dean Elliott Sep 14 '12 at 15:23
add comment

The parameter you are looking for is post__not_in (kaiser has a typo in his answer). So the code could be like:

<?php
    $my_query = new WP_Query(array(
        'post__not_in' => array(278),
        'post_type' => 'case-study',
        'paged' => $paged,
    ));
    while ($my_query->have_posts()) : $my_query->the_post();
?>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.