0

i want to show category list with custom post type count. but i have two different post types in every category. how can i do that . please help. this is my code:

<?php
    $category_object           = get_queried_object();
    $current_category_taxonomy  = $category_object->taxonomy;
    $current_category_term_id  = $category_object->term_id;
     $current_category_name  = $category_object->name;

    $args = array(
        'child_of'            => $current_category_term_id,
        'current_category'    => $current_category_term_id,
        'depth'               => 0,
        'echo'                => 1,
        'exclude'             => '',
        'exclude_tree'        => '',
        'feed'                => '',
        'feed_image'          => '',
        'feed_type'           => '',
        'hide_empty'          => 0,
        'hide_title_if_empty' => false,
        'hierarchical'        => true,
        'order'               => 'ASC',
        'orderby'             => 'name',
        'separator'           => '',
        'show_count'          => 1,
        'show_option_all'     => '',
        'show_option_none'    => __( 'No categories' ),
        'style'               => 'list',
        'taxonomy'            => 'category',
        'title_li'            => __( $current_category_name ),
        'use_desc_for_title'  => 0,
    );
    wp_list_categories($args);
 ?>
0

Out of the box this cannot be done, as the post counts on each term is singular, there is no breakdown by post type.

In order to do this, you will need to output the markup yourself, and query for the counts yourself.

This will be slow/heavy/expensive.

So first we start with the current term/taxonomy:

$category_object           = get_queried_object();
$current_category_taxonomy  = $category_object->taxonomy;
$current_category_term_id  = $category_object->term_id;
$current_category_name  = $category_object->name;

Then we grab all child terms that aren't empty:

$children = get_terms(
    [
        'taxonomy' => $current_category_taxonomy,
        'parent'   => $category_object->term_id,
    ]
);

And loop over each term:

foreach ( $children as $child ) {
    //
}

Now inside that loop we need to check how many posts are of your particular post type, lets say the changeme post type:

    $args = [
        'post_type'      => 'changeme',
        $term->taxonomy  => $child->term_id,
        'posts_per_page' => 1,
    ];
    $q    = new WP_Query( $args );
    if ( $q->have_posts() ) {
        echo '<li>' . esc_html( $term->name ) . ' ( '. intval( $q->found_posts ) . ' ) </li>';
    }

Notice we used the found_posts parameter. At this point, it's just a matter of wrapping the whole thing in a <ul> tag, and expanding the markup to match what you want. You have the term object inside the loop to print out URLs etc with.

Don't forget, this is expensive/slow/heavy, you will want to cache this, perhaps using a transient, or if you have an object cache or Elastic search present.

Or you could just register another taxonomy specific for this post type, and avoid all of this.

1
function my_taxonomy_posts_count_func($atts)
    {
        extract(shortcode_atts(array(
            'post_type' => 'post',
        ) , $atts));
        global $WP_Views;
        $term = $WP_Views->get_current_taxonomy_term();
        $args = array(
            'post_type' => $post_type,
            $term->taxonomy => $term->term_id,
            'order' => 'ASC',
            'posts_per_page' => - 1,
        );
        $posts = get_posts($args);
        if ($posts)
        {
            $res = count($posts);
        }
        return $res;
    
    }
    add_shortcode('my-taxonomy-posts-count', 'my_taxonomy_posts_count_func');
  1. add codes in your theme/functions.php

  2. put the shortcode in your content, like this:

    [my-taxonomy-posts-count post_type="my-custom-post-type"]

  3. replace the "my-custom-post-type" with your specific post type slug

  4. replace the shortcode [wpv-taxonomy-post-count] with [my-taxonomy-posts-count post_type="my-custom-post-type"]

Change code as per comment

8
  • thank you very much for your answer but it shows no category Aug 14 '20 at 9:41
  • $cats = wp_list_categories(); Aug 14 '20 at 9:42
  • echo wp_list_categories( array( 'orderby' => 'name', 'show_count' => true, 'taxonomy' => 'campaign_action' //i guess campaign_action is your taxonomy ) ); try this campaign_action change your taxonomy Aug 14 '20 at 9:46
  • yes it not taxonmy Aug 14 '20 at 9:47
  • can you give me screen short share your category list with url Aug 14 '20 at 9:49

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.