Ehsan
Ehsan

Reputation: 2285

exclude a post from wp_query loop wordpress

I show my last post in a page with this code:

$query1 = new WP_Query();
$query1->the_post();

and it further with:

$id = $query->ID;

to retrive last post ID so I wrote a new wp_query and I want to exclue that ID from the results: I wrote this but it don't work:

$query2-> new WP_Query('p=-$id');

what's the problem?

Upvotes: 1

Views: 3025

Answers (2)

Tusko Trush
Tusko Trush

Reputation: 430

My code is works fine:

$ID =array('1,2,3,4,5');
$news = new WP_Query(array('
    'post_type'    => 'post',
    'showposts'    =>3,
    'order'        => 'DESC',
    'post__not_in' => $ID
));
if ( $news->have_posts() ) :
    echo '<div>';
        while ( $news->have_posts() ) : $news->the_post(); ?>`

            //Your code here

Upvotes: 1

s_ha_dum
s_ha_dum

Reputation: 2850

You haven't excluded anything. Read the Codex. p= includes posts. It does not exclude them. What you need is post__not_in

$query2-> new WP_Query(array('post__not_in' = array($id)));

Upvotes: 2

Related Questions