Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 基于自定义分类法的Wordpress tax\u查询未显示任何结果_Php_Wordpress_Custom Taxonomy - Fatal编程技术网

Php 基于自定义分类法的Wordpress tax\u查询未显示任何结果

Php 基于自定义分类法的Wordpress tax\u查询未显示任何结果,php,wordpress,custom-taxonomy,Php,Wordpress,Custom Taxonomy,我有这个基于WordPress文档的查询,但没有结果。如果我删除tax_查询,它会给出所有的发布帖子,但是当我添加它时,它不会显示任何结果 我已经检查了数据库,我确信有帖子附在wp_term_分类法上,所以我不确定为什么它不显示结果 我在这里所做的是,获取所有自定义分类类型,然后循环遍历它以获取所有与之相关的帖子 $event_terms = get_terms( 'event_type', array( 'orderby'=>'name',

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

我已经检查了数据库,我确信有帖子附在wp_term_分类法上,所以我不确定为什么它不显示结果

我在这里所做的是,获取所有自定义分类类型,然后循环遍历它以获取所有与之相关的帖子

$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>";
    }
}
$event\u terms=get\u terms(
“事件类型”,
排列(
'orderby'=>'name',
“hide_empty”=>true
)
);
如果(!empty($event_terms)&&!is_wp_error($event_terms)){
foreach($event\u terms作为$event\u term){
//每个事件术语的代码
$args=数组(
“状态”=>“发布”,
“tax_query”=>数组(
排列(
“分类法”=>“事件类型”,
'字段'=>'段塞',
“术语”=>$event\u term->slug,
),
),
);
$loop=新的WP_查询($args);
如果($loop->have_posts()){
回显“.$event_term->name.”;
而($loop->have_posts()):$loop->the_post();
回声“
”; 结束时; } //这将显示所有分类术语 echo$event\u term->name.“-”$event\u term->term\u taxonomy\u id.“
”; } }
请像这样更改文本查询并尝试:

$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;

我希望这对您有所帮助。

您的代码似乎很好。可能是执行中的问题,也就是说在执行代码时,可能是分类法
事件类型未初始化。尝试在
wp
hook中运行上述代码,或在分类法初始化后触发的任何其他代码。@itzmekhokan您知道适合此的钩子吗?我在上面已经提到过。您可以使用
wp
hook-like
add_动作('wp','your_callback')我的意思是什么类型的钩子,我一直在使用add_action,顺便说一句,使用wp_加载的钩子,仍然没有得到。我用分类法对它进行了测试,它是有效的。