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.

$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;



// 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);


It 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(
            '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

  • thanks for your attention but problem is that i have about 20 taxonomy and 30 term in every taxonomy. thats why i want to get post count by month with get_queried – Raminer Dec 24 '19 at 11:21
  • It doesn't matter how many posts, taxonomies or terms you have, if you structure your query properly, you will get what you want. While testing, you can use: $thePosts = query_posts($args); and $thePosts ->found_posts; to get your results count. – TomC Dec 24 '19 at 11:47
  • Also, I think that this will help you: wordpress.stackexchange.com/questions/176141/… – TomC Dec 24 '19 at 11:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.