繁体   English   中英

WordPress。 如何在 taxonomy.php 中获取多个自定义 post_type 总帖子数?

[英]WordPress. How to get multiple custom post_type total posts count in taxonomy.php?

在我的 WordPress v5.7 taxonomy.php模板中,我有以下代码来获取自定义post_type总帖子数。 (此处建议的代码https://stackoverflow.com/a/66751447/3725816 )。

$term = get_queried_object();

$args = array(
    'post_type'      => 'your-post-type-name',
    'post_status'    => 'publish', // get only publish posts
    'posts_per_page' => -1, // get all posts
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'your-taxonomy-name',
            'field'    => 'term_id',
            'terms'    => $term->term_id
        )
    )
);

$AllPostByTerms = new WP_Query( $args );

echo $AllpostByTerms->post_count;

现在我的 WordPress 中有多个(许多)自定义 post_type。

有没有一种方法可以在单个 function / 查询中获取每个自定义post_type的总帖子数,而不是为taxonomy.php模板中的每个 post_type 重复相同的代码?

$args = array(
'post_type'      => ['post-type-name1', 'post-type-name2', 'post-type-name3'],
'post_status'    => 'publish', // get only publish posts
'posts_per_page' => -1, // get all posts
'tax_query' => array(
    'relation' => 'AND',
    array(
        'taxonomy' => 'your-taxonomy-name',
        'field'    => 'term_id',
        'terms'    => $term->term_id
    )
)

);

您可以简单地在帖子类型参数中传递所有自定义帖子类型的数组

暂无
暂无

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

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