Get post ID's from one query and exclude from another

I have 2 instances of the WP_Query class, one showing 4 "Featured" posts and another showing "basic"

I have 2 instances of the WP_Query class, 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?

I have 2 instances of the WP_Query class, 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 edited Jan 6, 2020 at 15:54 Viktor Borítás 3042 silver badges11 bronze badges asked Jul 29, 2013 at 8:13 PoisontonomesPoisontonomes 6064 gold badges17 silver badges28 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

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();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744856929a4597463.html

相关推荐

  • Get post ID's from one query and exclude from another

    I have 2 instances of the WP_Query class, one showing 4 "Featured" posts and another showing "basic"

    2天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信