我正在尝试从“最近的帖子”页面中排除标记为“精选”的帖子的方法。
如下所示,“我最近的帖子”页面仅显示2类(cat1和cat2)中的帖子。问题是,我偶尔会在以下类别之一中添加帖子,并将其标记为“功能链接”或其他。因此,当您查看页面时,您基本上会在标题(循环的外部)和正文(循环的内部)中看到特色帖子。我将如何删除循环中的特色帖子?
<?php
// args query
$args = array(
'post_type' => 'post',
'posts_per_page' => 3,
'order' => 'DESC',
// display only posts in specifics categories (slug)
'category_name' => 'cat1, cat2'
);
// custom query
$recent_posts = new WP_Query($args);
// check that we have results
if($recent_posts->have_posts()) : ?>
<?php
// start loop
while ($recent_posts->have_posts() ) : $recent_posts->the_post();
?>
对于任何想知道的人,我尝试了以下方法,但是没有用:
$args = array(
'post_type' => 'post',
'posts_per_page' => 3,
'order' => 'DESC',
// display only posts in specifics categories (slug)
'category_name' => 'cat1, cat2',
'meta_query' => array(
array(
'key' => 'featured',
'value' => 'yes',
'compare' => 'NOT LIKE',
),
),
);
添加其他人建议的“ meta_query”内容时,页面似乎停止一起显示结果。
是否有关于如何正确编写此文字的想法,也许还提供其他替代解决方案?
谢谢。
答案 0 :(得分:0)
经测试工作正常。只需将'compare' => 'NOT LIKE'
替换为'compare' => 'NOT EXISTS'
。希望对您有帮助。
// args
$args = array(
'numberposts' => 3,
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'featured',
'value' => 'yes',
'compare' => 'NOT EXISTS',
),
),
);
// query
$the_query = new WP_Query( $args );
if( $the_query->have_posts() ):
while( $the_query->have_posts() ) : $the_query->the_post();
echo the_title();
endwhile;
endif;
wp_reset_query();
答案 1 :(得分:0)
另一种方法是您可以在'post__not_in' => array (//post ids you want to exclude)
中使用wp_query