WordPress计算分类法的简码[标签]

问题描述

问题
我想为具有CPT => toernooi的WordPress网站创建简码 帖子计数功能使用以下代码

    // Get post count for cpt
add_shortcode('toernooiencount','post_count');
function post_count() {
    $count_posts = wp_count_posts('toernooi');
    $published_posts = $count_posts->publish;
    echo $published_posts . ' ';
}

现在,我想创建相同的功能来对职位进行计数,但要在同一CPT中使用不同的分类法。试图找到解决方案,但找不到任何有效的方法WordPress网站wp_count_posts没有有关使用不同用例的信息。

问题
谁能指出我正确的方向?

解决方法

我认为最简单的方法就是像这样使用WP_Query:

$args  = array(
    'post_type'      => 'toernooi','post_status'    => 'publish','posts_per_page' => - 1,'tax_query'      => array(
        array(
            'taxonomy' => 'your_taxonomy','field'    => 'slug',//('id' o 'slug')
            'terms'    => 'my_term_slug_or_id',),);
$query = new WP_Query( $args );

echo "We have found {$query->post_count} posts witht the query";

我想不出另一种解决方案,因为wp_query在内部始终会执行“ SELECT SQL_CALC_FOUND_ROWS”,因此您无需支付任何其他费用就可以算出