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
总帖子数。 (此处建议的代码位于 )。
$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。
不是为 taxonomy.php
模板中的每个 post_type 重复相同的代码,有没有办法在单个函数中获取每个自定义 post_type
的帖子总数 /查询?
编辑(期望的结果):
我希望每个自定义 post_type 总帖子计数。
示例:
post-type1 = 10,
post-type-name2 = 20,
post-type-name3 = 30
$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
)
)
);
您可以简单地在 post 类型参数
中传递所有自定义 post 类型的数组
所以我偶然发现了你的问题,不确定你是否仍然有兴趣看到答案,但我会添加我的答案以供将来参考,以防万一有人需要它。
因此,您可以通过多种方式实现您的目标!但是,有些事情您在问题中没有提到!
例如,在计算“总 post 计数”时,您没有提及您指的是哪个 post 状态!
- Counting published posts?
- Counting draft posts?
- Counting pending posts?
- Counting trashed posts?
- Counting future posts?
- Counting private posts?
您的问题中另一件不清楚的事情是您有兴趣计算的“post 的类型”!
比如除了你的custom-post-type-1,custom-post-type-2,custom-post-type-3,wordpress还有它自己的“内置”post 类型,例如:
- Post
- Page
- Attachment
- Nav_menu_item
- Revision
我给你我的答案,欢迎根据需要自定义!
由于您没有提及您要查找的具体“post 类型”,我假设您只对 custom-post-type-1
、custom-post-type-2
和 custom-post-type-3
等。因此,我们将使用一个名为 get_post_types
的 wordpress 函数来动态获取我们感兴趣的所有自定义 post 类型。另外,我假设您有兴趣数 all of the post statuses
。为此,我将使用一个名为 wp_count_posts
.
的 wordpress 函数
现在使用上述函数,让我们创建我们的 神奇函数 它将 return a multidimentional associative array
包含每个自定义的每个状态的计数 post 输入!
不需要 WP_QUERY!
function your_them_counter_post_types()
{
$args = array(
'public' => true,
'_builtin' => false // This will leave out built-in post types (i.e Post, Page, Attachment, Nav_menu_item, Revision)
);
$output = 'names';
$operator = 'and';
$post_types = get_post_types($args, $output, $operator);
$counts_array = array();
if ($post_types) {
if (!is_array($post_types)) {
$post_types = (array)$post_types;
} // just to make sure our 'foreach' doesn't throw an error!
foreach ($post_types as $post_type) {
$current_post_type = wp_count_posts($post_type);
$counts_array[$post_type] = array(
'published' => intval($current_post_type->publish),
'draft' => intval($current_post_type->draft),
'pending' => intval($current_post_type->pending),
'future' => intval($current_post_type->future),
'private' => intval($current_post_type->private),
'trash' => intval($current_post_type->trash)
);
}
}
return $counts_array;
}
这将 return 一个 multidimentional associative array
看起来像这样:
$counts_array
(
[custom-post-type-1] => Array
(
[published] => 10,
[draft] => 2,
[pending] => 3,
[future] => 0,
[private] => 0,
[trash] => 1
),
[custom-post-type-2] => Array
(
[published] => 20,
[draft] => 5,
[pending] => 4,
[future] => 0,
[private] => 0,
[trash] => 2
),
[custom-post-type-3] => Array
(
[published] => 30,
[draft] => 7,
[pending] => 1,
[future] => 0,
[private] => 0,
[trash] => 3
)
)
现在我们有了一个 神奇的函数,我们需要做的就是调用它并循环遍历 returned 数组,如下所示:
$posts_counts_array = your_them_counter_post_types();
foreach ($posts_counts_array as $post_type_name => $post_count_array) {
printf(
__('For %s: ', 'Your-Theme-text-domain'),
$post_type_name
);
echo "<pre>";
foreach ($post_count_array as $post_status_name => $post_counts) {
printf(
_n(
'There is %1$s %2$s post.',
'There are %1$s %2$s posts.',
$post_counts,
'Your-Theme-text-domain'
),
$post_counts,
$post_status_name
);
echo "<br>";
}
echo "</pre>";
}
这将 return 您所要求的!
For custom-post-type-1:
There are 10 published posts.
There are 2 draft posts.
There are 3 pending posts.
There are 0 future posts.
There are 0 private posts.
There is 1 trash post.
For custom-post-type-2:
There are 20 published posts.
There are 5 draft posts.
There is 1 pending post.
There are 0 future posts.
There are 0 private posts.
There are 2 trash posts
For custom-post-type-3:
There are 30 published posts.
There are 7 draft posts.
There is 1 pending post.
There are 0 future posts.
There are 0 private posts.
There are 3 trash posts
Note:
I've also used the following functions to do the translations and printing the values onto the page:
__
Docs
_n
Docs
printf
Docs
在我的 WordPress v5.7 taxonomy.php
模板中,我使用以下代码来获取自定义 post_type
总帖子数。 (此处建议的代码位于
$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。
不是为 taxonomy.php
模板中的每个 post_type 重复相同的代码,有没有办法在单个函数中获取每个自定义 post_type
的帖子总数 /查询?
编辑(期望的结果):
我希望每个自定义 post_type 总帖子计数。
示例:
post-type1 = 10,
post-type-name2 = 20,
post-type-name3 = 30
$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
)
)
);
您可以简单地在 post 类型参数
中传递所有自定义 post 类型的数组所以我偶然发现了你的问题,不确定你是否仍然有兴趣看到答案,但我会添加我的答案以供将来参考,以防万一有人需要它。
因此,您可以通过多种方式实现您的目标!但是,有些事情您在问题中没有提到!
例如,在计算“总 post 计数”时,您没有提及您指的是哪个 post 状态!
- Counting published posts?
- Counting draft posts?
- Counting pending posts?
- Counting trashed posts?
- Counting future posts?
- Counting private posts?
您的问题中另一件不清楚的事情是您有兴趣计算的“post 的类型”!
比如除了你的custom-post-type-1,custom-post-type-2,custom-post-type-3,wordpress还有它自己的“内置”post 类型,例如:
- Post
- Page
- Attachment
- Nav_menu_item
- Revision
我给你我的答案,欢迎根据需要自定义!
由于您没有提及您要查找的具体“post 类型”,我假设您只对 custom-post-type-1
、custom-post-type-2
和 custom-post-type-3
等。因此,我们将使用一个名为 get_post_types
的 wordpress 函数来动态获取我们感兴趣的所有自定义 post 类型。另外,我假设您有兴趣数 all of the post statuses
。为此,我将使用一个名为 wp_count_posts
.
现在使用上述函数,让我们创建我们的 神奇函数 它将 return a multidimentional associative array
包含每个自定义的每个状态的计数 post 输入!
不需要 WP_QUERY!
function your_them_counter_post_types()
{
$args = array(
'public' => true,
'_builtin' => false // This will leave out built-in post types (i.e Post, Page, Attachment, Nav_menu_item, Revision)
);
$output = 'names';
$operator = 'and';
$post_types = get_post_types($args, $output, $operator);
$counts_array = array();
if ($post_types) {
if (!is_array($post_types)) {
$post_types = (array)$post_types;
} // just to make sure our 'foreach' doesn't throw an error!
foreach ($post_types as $post_type) {
$current_post_type = wp_count_posts($post_type);
$counts_array[$post_type] = array(
'published' => intval($current_post_type->publish),
'draft' => intval($current_post_type->draft),
'pending' => intval($current_post_type->pending),
'future' => intval($current_post_type->future),
'private' => intval($current_post_type->private),
'trash' => intval($current_post_type->trash)
);
}
}
return $counts_array;
}
这将 return 一个 multidimentional associative array
看起来像这样:
$counts_array
(
[custom-post-type-1] => Array
(
[published] => 10,
[draft] => 2,
[pending] => 3,
[future] => 0,
[private] => 0,
[trash] => 1
),
[custom-post-type-2] => Array
(
[published] => 20,
[draft] => 5,
[pending] => 4,
[future] => 0,
[private] => 0,
[trash] => 2
),
[custom-post-type-3] => Array
(
[published] => 30,
[draft] => 7,
[pending] => 1,
[future] => 0,
[private] => 0,
[trash] => 3
)
)
现在我们有了一个 神奇的函数,我们需要做的就是调用它并循环遍历 returned 数组,如下所示:
$posts_counts_array = your_them_counter_post_types();
foreach ($posts_counts_array as $post_type_name => $post_count_array) {
printf(
__('For %s: ', 'Your-Theme-text-domain'),
$post_type_name
);
echo "<pre>";
foreach ($post_count_array as $post_status_name => $post_counts) {
printf(
_n(
'There is %1$s %2$s post.',
'There are %1$s %2$s posts.',
$post_counts,
'Your-Theme-text-domain'
),
$post_counts,
$post_status_name
);
echo "<br>";
}
echo "</pre>";
}
这将 return 您所要求的!
For custom-post-type-1:
There are 10 published posts.
There are 2 draft posts.
There are 3 pending posts.
There are 0 future posts.
There are 0 private posts.
There is 1 trash post.
For custom-post-type-2:
There are 20 published posts.
There are 5 draft posts.
There is 1 pending post.
There are 0 future posts.
There are 0 private posts.
There are 2 trash posts
For custom-post-type-3:
There are 30 published posts.
There are 7 draft posts.
There is 1 pending post.
There are 0 future posts.
There are 0 private posts.
There are 3 trash posts
Note:
I've also used the following functions to do the translations and printing the values onto the page:
__
Docs
_n
Docs
printf
Docs