Wordpress - 显示所有帖子+具有特定标签的帖子

时间:2013-10-23 09:39:03

标签: php wordpress sorting filter tags

我有3种类型的帖子,如下所示

  1. 没有任何标记的普通帖子
  2. 使用“精选”标签
  3. 使用“已售出”标签
  4. 在某个页面上,我只想显示正常帖子+带有精选标签的帖子,并且不希望显示带有“已售出”标签的帖子。我该如何进行查询呢?

    由于

2 个答案:

答案 0 :(得分:0)

您可以将get_posts功能与tax_query参数一起使用。

因为它非常简单,我认为我不应该再写任何东西,只需阅读 来自codex的下一部分:

http://codex.wordpress.org/Template_Tags/get_posts#Taxonomy_Parameters

答案 1 :(得分:0)

您最好使用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