从wp_query中排除帖子ID
4 个回答
- 投票数
-
- 2012-09-14
我想这很沉重,但是为了回答您的原始问题,我在第一个循环中将所有帖子ID收集在一个数组中,并使用'post__not_in'在第二个循环中排除了这些帖子strong>,它需要一个帖子ID的数组
<?php $args1 = array('category_name' => 'test-cat-1', 'order' => 'ASC'); $q1 = new WP_query($args); if($q1->have_posts()) : $firstPosts = array(); while($q1->have_posts()) : $q1->the_post(); $firstPosts[] = $post->ID; // add post id to array echo '<div class="item">'; echo "<h2>" . get_the_title() . "</h2>"; echo "</div>"; endwhile; endif; /****************************************************************************/ // array of post id's collected in first loop, can now be used as value for the 'post__not_in' parameter in second loops query $args $args2 = array('post__not_in' => $firstPosts, 'order' => 'ASC' ); $q2 = new WP_query($args2); if($q2->have_posts()) : while($q2->have_posts()) : $q2->the_post(); echo '<div class="item">'; echo "<h2>" . get_the_title() . "</h2>"; echo "</div>"; endwhile; endif; ?>
第一个循环显示类别中的所有帖子,并将帖子ID收集到一个数组中.
第二个循环显示所有帖子,不包括第一个循环的帖子.
I suppose this was heavy, but to answer your original question, I've collected all of the posts id's in an array in the first loop, and excluded those posts from the second loop using 'post__not_in' which expects an array of post id's
<?php $args1 = array('category_name' => 'test-cat-1', 'order' => 'ASC'); $q1 = new WP_query($args); if($q1->have_posts()) : $firstPosts = array(); while($q1->have_posts()) : $q1->the_post(); $firstPosts[] = $post->ID; // add post id to array echo '<div class="item">'; echo "<h2>" . get_the_title() . "</h2>"; echo "</div>"; endwhile; endif; /****************************************************************************/ // array of post id's collected in first loop, can now be used as value for the 'post__not_in' parameter in second loops query $args $args2 = array('post__not_in' => $firstPosts, 'order' => 'ASC' ); $q2 = new WP_query($args2); if($q2->have_posts()) : while($q2->have_posts()) : $q2->the_post(); echo '<div class="item">'; echo "<h2>" . get_the_title() . "</h2>"; echo "</div>"; 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.
-
另一方面,有没有一种方法可以将wp-pagenavi添加到第二个查询中?On another note, Is there a way to add wp-pagenavi to the 2nd query?
- 0
- 2012-09-15
- Dean Elliott
-
万一您重新回答了您的问题:请修正您的代码标记/意图.谢谢.In case you ever revisit your answer: Please fix your code markup/intending. Thanks.
- 1
- 2014-10-12
- kaiser
-
- 2013-05-10
您要查找的参数是
post__not_in
(kaiser的答案有错字).所以代码可能像这样:<?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(); endwhile;
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(); endwhile;
-
您知道,有[edit]用于纠正错别字:)You know, there are [edit]s for correcting typos :)
- 3
- 2014-10-12
- kaiser
-
@Ziki数组中的逗号不是错字,它是有效的PHP语法,如果那是您的意思.@Ziki the comma in the array is not a typo it is valid PHP syntax, if thats what you mean.
- 0
- 2017-06-21
- Leo
-
@leonziyo-不,他原来那里是"posts__not_in"而不是"post__not_in",请参阅他的回答的历史记录.昏迷没事@leonziyo - no, he originally had there "posts__not_in" instead of "post__not_in", see history of his answer. Coma is fine
- 1
- 2017-06-22
- Ziki
-
- 2012-09-14
您必须将
post__not_in
arg定义为数组.即使是单个值.并且请不要用临时的东西覆盖全局核心变量.<?php $query = new WP_Query( array( 'post_type' => 'case-study', 'paged' => $paged, 'post__not_in' => array( 1, ), ) ); if ( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post(); // do stuff } // endwhile; } // endif; ?>
You have to define the
post__not_in
arg as array. Even for a single value. And please don't overwrite global core variables with temporary stuff.<?php $query = new WP_Query( array( 'post_type' => 'case-study', 'paged' => $paged, 'post__not_in' => array( 1, ), ) ); if ( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post(); // do stuff } // endwhile; } // endif; ?>
-
- 2019-03-22
备用代码;
排除类别帖子
<?php add_action('pre_get_posts', 'exclude_category_posts'); function exclude_category_posts( $query ) { if($query->is_main_query() && $query->is_home()) { $query->set('cat', array( -22, -27 )); } }
从主页页面删除帖子
<?php add_action('pre_get_posts', 'wpsites_remove_posts_from_home_page'); function wpsites_remove_posts_from_home_page( $query ) { if($query->is_main_query() && $query->is_home()) { $query->set('category__not_in', array(-1, -11)); } }
Alternative codes;
Exclude category posts
<?php add_action('pre_get_posts', 'exclude_category_posts'); function exclude_category_posts( $query ) { if($query->is_main_query() && $query->is_home()) { $query->set('cat', array( -22, -27 )); } }
Remove posts from homepage page
<?php add_action('pre_get_posts', 'wpsites_remove_posts_from_home_page'); function wpsites_remove_posts_from_home_page( $query ) { if($query->is_main_query() && $query->is_home()) { $query->set('category__not_in', array(-1, -11)); } }
如何从WP_Query查询中排除一个特定的帖子?(例如,显示除ID为278的帖子以外的所有帖子)
我已经尝试过post__not_in参数,但是它只是删除了所有帖子.
任何帮助都会很棒.
这是我当前的查询
谢谢