【问题标题】:Wordpress - show all posts + ones with specific tagWordpress - 显示所有帖子 + 带有特定标签的帖子
【发布时间】:2013-11-01 12:27:46
【问题描述】:

我有以下 3 种类型的帖子

  1. 没有任何标签的普通帖子
  2. 带有“特色”标签
  3. 带有“已售”标签

在页面上,我只想显示普通帖子+带有特色标签的帖子,不想显示带有“已售”标签的帖子。我该如何查询这个?

谢谢

【问题讨论】:

  • 你的问题解决了吗?

标签: php wordpress sorting filter tags


【解决方案1】:

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

因为它很简单,我不认为我应该再写任何东西,只需阅读 法典的下一部分:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2015-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多