如何查询在wordpress中按标签名称检索自定义帖子标题

时间:2016-03-15 09:29:58

标签: wordpress

我想通过标签名称检索自定义帖子标题。我有标签名称,我想要检索标题。我该怎么做。

 <?php
        $tagname=get_post_meta(get_post()->ID, 'campaign_tag', true);
        echo $tagname;
        $args = array(
            'post_type' => 'campaign',
            'posts_per_page' => 20,
            'tag' => $tagname,
        );

        $query = new WP_Query($args);
        while($query -> have_posts()) : $query -> the_post();
    ?>
    <h5><?php the_title(); ?></h5>
    <?php endwhile; wp_reset_query(); 
    ?>

1 个答案:

答案 0 :(得分:1)

对于CPT(自定义帖子类型)您应该使用tax_query。 像那样:

$args = array(
   'post_type' => 'campaign',
   'tax_query' => array(
              array(
                 'taxonomy' => 'tax_name',
                 'field'    => 'slug',
                 'terms'    => 'bob',
              ),
),); $query = new WP_Query( $args );

参考:https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters