首页 > 解决方案 > 显示一个类别中的事件总数 - 事件日历 Wordpress 插件

问题描述

我正在使用The Events Calendar Wordpress 插件并尝试显示一个类别中的事件总数。目前正在尝试这个:

$args = array(
    'post_type' => 'tribe_events',
    'tax_query' => array(
        array(
            'taxonomy' => 'tribe_events_cat',
            'field' => 'food',
        ),
    ),
);
$query = new WP_Query( $args );

然后使用它来显示结果:

<?php echo $query-> found_posts; ?>

我的输出虽然返回 0。我错过了什么吗?

标签: phpwordpresscustom-wordpress-pages

解决方案


弄清楚了!看起来我的第一个 tax_query 数组属性有点偏离。这是修复:

$args = array(
    'post_type' => 'tribe_events',
    'tax_query' => array(
        array(
            'taxonomy' => Tribe__Events__Main::TAXONOMY,
            'field' => 'slug',
            'terms' => 'food'
        ),
    ),
);

回显相同的查询显示了正确的帖子数量。


推荐阅读