1

I am trying to create a shortcode to display the posts count in a category. I have successfully done this using this code:

// Add Shortcode to show posts count inside a category
function add_count_of_posts_in_category() {
$term = get_term( 7, 'category' ); 
$count = $term->count;
echo $count;
}
add_shortcode( 'show-posts-count', 'add_count_of_posts_in_category' );

However, this means that I need to specify the ID of the category for the shortcode to work. Which means that i need to create a shortcode per category, which is useless.

I am trying to find a way to modify the category ID part with a variable, so that I can use the shortcode like this: [show-posts-count="cars"] to display the post count in the category called cars. I can't find a way to do so.

Your help is much appreciated.

EDIT: 29/09/2016 After getting the code working, I am trying to expand the function to count the posts in the child category too.

So if the main category does not have posts, but has 2 subcategories, each has posts, then when I use the shortcode on the main category, the displayed number is the sum of all the posts in the main category (if any), in addition to, the number of posts in the sub-categories, and the sub-sub-categories..etc

I tried using get_term_children( $term, $taxonomy );, but did not know how to get the post count of subcategories then add them together.

2

The shortcode

// Add Shortcode to show posts count inside a category
function category_post_count( $atts ) {

    $atts = shortcode_atts( array(
        'category' => null
    ), $atts );

    // get the category by slug.
    $term = get_term_by( 'slug', $atts['category'], 'category');

    return ( isset( $term->count ) ) ? $term->count : 0;
}
add_shortcode( 'category_post_count', 'category_post_count' );

Usage

[category_post_count category="category_slug_or_name"]

If you want to get the count by name, not slug change this

$term = get_term_by( 'slug', $atts['category'], 'category');

to this:

$term = get_term_by( 'name', $atts['category'], 'category');
  • Thank you very much for the quick solution. The code worked perfectly for the slug. When I tried the name it returned 0. any idea why? – Atef Wagih Sep 28 '16 at 0:17
  • You have to make sure that the category name is correct and match an existing category name. If a code you tried didn't work, please edit your question and include the non-working function and I'll have a look at it. – Ahmed Fouad Sep 28 '16 at 0:20
  • I edited the question with the required details. Thanks – Atef Wagih Sep 28 '16 at 0:50
  • Can you try to wrap the category name with quotes and see if that helps? e.g. category="My category name" instead of category=My category name. Having two-word category will mislead Wordpress shortcodes to think that the 2nd word is a parameter. so category=" ... " (with quotes) should work :) – Ahmed Fouad Sep 28 '16 at 0:53
  • thank you very much. now it works perfectly fine. Much appreciated. – Atef Wagih Sep 28 '16 at 1:12
0

To count posts in subcategories in addition to specified category, one possibility is to query posts using a cat option and count the results. cat option queries posts in child categories by default.

add_shortcode('category_post_count', function ($atts, $content) {
    $atts = shortcode_atts([
        'category' => 0
    ], $atts);
    $cat_id = is_numeric($atts['category']) ?
        intval($atts['category']) :
        get_category_by_slug($atts['category'])->term_id;
    return count(get_posts([
        'nopaging' => true,
        'fields' => 'ids',
        'cat' => $cat_id
    ]));
});

There are examples of querying the child categories and summing up the count fields, but this may yield invalid results if a post belongs to more than one of the matched categories.

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.