您只需从循环中构建一个帖子ID数组,然后从以下查询集中排除这些帖子
我不知道您的确切结构,但这里有一个非常基本的想法(需要PHP5.4+,因为数组语法很短([]
), 对于旧版本,请更改回旧的数组语法(array()
))
// Define the variable to hold posts
$remove_duplicates = [];
// define and run our first query
$args1 = [
// All your arguments for this query
];
$loop1 = new WP_Query( $args1 );
if ( $loop1->have_posts() ) {
while ( $loop1->have_posts() ) {
$loop1->the_post();
// Build our array of ID\'s to exclude from other query
$remove_duplicates[] = get_the_ID();
// Rest of your loop
}
wp_reset_postdata(); // VERY VERY IMPORTANT
}
// Define our second query and exclude posts from the first
$args2 = [
\'post__not_in\' => $remove_duplicates, // Remove posts from from $loop1
// All your arguments for this query
];
$loop2 = new WP_Query( $args2 );
if ( $loop2->have_posts() ) {
while ( $loop2->have_posts() ) {
$loop2->the_post();
// Rest of your loop
}
wp_reset_postdata(); // VERY VERY IMPORTANT
}