您好,我想按月显示当前分类法职位类型计数,但它显示当前税务的总职位计数。请帮帮我。我正在努力解决这个问题将近三个星期。
<?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);
}
?>
SO网友:TomC
覆盖时,您的查询似乎是针对该帖子类型中的所有帖子$posts
使用时$posts = get_posts()
.您需要按照说明正确传递分类参数here 像这样:
$args = array(
\'post_type\' => \'post\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'people\',
\'field\' => \'slug\',
\'terms\' => \'bob\',
),
),
);
$query = new WP_Query( $args );
您需要按照这些思路修改代码。所以也许会改变
$posts = get_queried_object()->term_id;
到
$termID = get_queried_object()->term_id;
在你的
tax_query