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.

I have 2 instances of wp_query, one showing 4 "Featured" posts and another showing "basic" posts.

What I want to do is exclude those 4 latest "Featured" posts from the basic query. I assume it's just a case of adding a post__not_in variable but how can I get the ID's from the first query and exclude them from the second?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

While running the loop of the first query, just collect the IDs into an array. Example:

// define your first query's arguments
$args1 = array(
    ... your first query args
);
$query1 = new WP_Query($args1);
while ( $query1->have_posts() ): $query1->the_post();
    // this is your loop. Do whatever you want here
    // add this in the loop to collect the post IDs
    $exclude_posts[] = $post->ID;
endwhile;

// define your second query's arguments
$args2 = array(
    'post__not_in' => $exclude_posts,
    ... rest of your parameters
);
$query2 = new WP_Query($args2);
while ( $query2->have_posts() ): $query2->the_post();
    // this is your second loop. Do whatever you want here
endwhile;
wp_reset_postdata();
share|improve this answer
    
Well, your code had some mistakes. Unclosed brackets, undefined variables ($query - so it was causing infinite loops), and redundant wp_reset_query calls. It should be OK now. –  Krzysiek Dróżdż Jul 29 '13 at 9:17
    
Krzysiek Dróżdż: I was not attempting to write full code but rather display the principle of it. –  Biranit Goren Jul 29 '13 at 9:38
    
I know. But you shouldn't make mistakes in it. Unclosed brackets, using undefined variables ($query instead of $query1) are errors, not simplifications... –  Krzysiek Dróżdż Jul 29 '13 at 9:40
    
Point taken. Thanks for your effort. –  Biranit Goren Jul 29 '13 at 9:42

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.