繁体   English   中英

Wordpress tax_query 基于自定义分类法未显示任何结果

[英]Wordpress tax_query based on custom taxonomy not showing any result

我有这个基于 WordPress 文档的查询,但没有结果。 如果我删除 tax_query 它会提供所有发布帖子,但是当我添加它时,它不会显示任何结果。

我检查了数据库,我确定 wp_term_taxonomy 附有帖子,所以我不确定它为什么不显示结果。

我在这里做什么,我得到所有自定义分类类型,然后遍历它以获取与之相关的所有帖子。

$event_terms = get_terms(
    'event_type',
    array(
        'orderby'=>'name',
        'hide_empty'=>true
    )
);

if(!empty($event_terms) && !is_wp_error($event_terms)){
    foreach($event_terms as $event_term){
        // code for each event term
        $args = array(
            'status' => 'publish',
            'tax_query' => array(
                array(
                    'taxonomy' => 'event_type',
                    'field'    => 'slug',
                    'terms'    => $event_term->slug,
                ),
            ),
        );

         $loop = new WP_Query($args);
         if($loop->have_posts()) {

            echo '<h2>'.$event_term->name.'</h2>';

            while($loop->have_posts()) : $loop->the_post();
                echo '<a href="'.get_permalink().'">'.get_the_title().'</a><br>';
            endwhile;
         }

    // this displays all taxonomy terms
    echo $event_term->name."-".$event_term->term_taxonomy_id."<br>";
    }
}

请像这样更改您的文本查询并尝试:

$the_query = new WP_Query( array(
    'post_type' => 'post',
    'tax_query' => array(
    array(
       'taxonomy' => 'event_type',
       'field' => 'slug',
       'terms' => $event_term->slug
    )
  )
) );
$count = $the_query->found_posts;

我希望这对你有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM