我有 3 种类型的帖子如下
- 没有任何标签的普通帖子
- 带"精选"标签
- 带有"已售出"标签
在页面上,我只想显示普通帖子 + 带有精选标签的帖子,而不想显示带有"已售出"标签的帖子。如何对此进行查询?
谢谢
您可以将 get_posts
函数与 tax_query
参数一起使用。
因为它很简单,我认为我不应该再写任何东西,只是阅读法典的下一部分:
http://codex.wordpress.org/Template_Tags/get_posts#Taxonomy_Parameters
你最好使用 WP_Query
,做这样的事情:
// you'll need the term_id of the tags you would like to exclude
$sold_tag = get_term_by('name','sold','post_tag');
$featured_tag = get_term_by('name','featured','post_tag');
// create a query object, this will pick all posts except the ones tagged with 'sold'
// if you wanted to pick all post marked as sold or featured and everyone not marked
// as for instance 'fruit' + all that isn't tagged at all you could use a combination of
// tag__not_in => array($fruit->term_id) && tag => array('featured,sold')
//
$query = new WP_Query(
array('post_type' => 'post',
'tag__not_in' => array(
$sold_tag->term_id
)
)
);
// start the loop
while($query->have_posts()): $query->the_post();
// output post here ...
endwhile;
阅读更多 关于 WP_Query