admin管理员组文章数量:1405186
Hi i want to show current taxonomy post type count by month but it show total post count istead of current tax. please help me. i am trying to solve this problem almost 3 weeks.
<?php
$post_type = "myposttype";
$timestamp_start = strtotime("first day of next month -1 year midnight");
$start = date("Y-m-d H:i:s", $timestamp_start);
$posts = get_posts([
"nopaging" => TRUE,
"post_type" => $post_type,
"date_query" => [
"after" => $start,
],
]);
// sort by month
$tab = [];
foreach ($posts as $post) {
$month = mysql2date("F", $post->post_date);
if (!isset($tab[$month])) {
$tab[$month] = 0;
}
$tab[$month]++;
}
// display
$timestamp = $timestamp_start;
$now = time();
while ($timestamp < $now) {
$month = date_i18n("F", $timestamp);
$count = $tab[$month] ?? 0; // need PHP 7
echo '<li>'."$month: $count". '</li>';
// next month
$timestamp = strtotime("+1 month", $timestamp);
}
?>
Hi i want to show current taxonomy post type count by month but it show total post count istead of current tax. please help me. i am trying to solve this problem almost 3 weeks.
<?php
$post_type = "myposttype";
$timestamp_start = strtotime("first day of next month -1 year midnight");
$start = date("Y-m-d H:i:s", $timestamp_start);
$posts = get_posts([
"nopaging" => TRUE,
"post_type" => $post_type,
"date_query" => [
"after" => $start,
],
]);
// sort by month
$tab = [];
foreach ($posts as $post) {
$month = mysql2date("F", $post->post_date);
if (!isset($tab[$month])) {
$tab[$month] = 0;
}
$tab[$month]++;
}
// display
$timestamp = $timestamp_start;
$now = time();
while ($timestamp < $now) {
$month = date_i18n("F", $timestamp);
$count = $tab[$month] ?? 0; // need PHP 7
echo '<li>'."$month: $count". '</li>';
// next month
$timestamp = strtotime("+1 month", $timestamp);
}
?>
Share
Improve this question
edited Dec 24, 2019 at 11:19
Raminer
asked Dec 24, 2019 at 9:43
RaminerRaminer
334 bronze badges
1 Answer
Reset to default 1It looks like your query is for ALL posts in that post type as you are overwriting $posts
when using $posts = get_posts()
.
You need to pass the Taxonomy parameter correctly as explained here like this:
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'people',
'field' => 'slug',
'terms' => 'bob',
),
),
);
$query = new WP_Query( $args );
You need to modify your code along those lines. So maybe change $posts = get_queried_object()->term_id;
to $termID = get_queried_object()->term_id;
and use that in your tax_query
本文标签: custom post typesQuery not work for current taxonomy
版权声明:本文标题:custom post types - Query not work for current taxonomy 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744881622a2630240.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论