custom post type category count shortcode

I’d like to create a shortcode that would display the post count for a specific category within a custom post type. I’ve been trying to alter the code below but need some help. Could anyone let me know how to tweak so that the shortcode is for a specific custom post type.

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

Thanks in advance!

WordPress Development Asked by Northtime on November 11, 2021

2 Answers

2 Answers

If for some reason you're using the shortcode inside the loop, you could try the code below. Failing that, your best bet is to get to grips with WP_Query as suggested in another answer.

Add to functions.php

function category_post_count( $atts ) {

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

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

    if( get_post_type() == $tpt ) {
        return $term->count;

add_shortcode( 'category_post_count', 'category_post_count' );

Typical usage

[category_post_count category="category_slug" type="post_type"]

Answered by Nikki Mather on November 11, 2021

I think you would be better off using get_posts or a new WP_Query instead of get_term_by. That would allow you to get posts for your custom post type, and then filter by taxonomy/term name.

If you're comfortable enough with WP Query you could try something like this: Query posts by taxonomy term name

Answered by Charlie Stanard on November 11, 2021

Add your own answers!

Related Questions

List posts based on first letter of posts

2  Asked on September 13, 2020 by shahinul-islam


How to redirect a page to another?

1  Asked on September 13, 2020 by heera


How to get the meta title of a page configured as blog (loop)

1  Asked on September 8, 2020 by gerard


I am trying to match the wpallimport using the Xpath Filter

1  Asked on September 4, 2020 by ankit-prajapati


how to list all post that are in the custom taxonomy using $wpdb

0  Asked on September 3, 2020 by kenneth-gervacio


How to pass multiple values in a form for tax_query?

0  Asked on August 21, 2020 by irishrunner16


How to add post_distinct filter to WP_Comment_Query?

2  Asked on August 17, 2020 by jonathan-gruber


How can I combine one field using wpdb and group by?

1  Asked on August 10, 2020 by user81828


Custom posts password protect

1  Asked on August 6, 2020 by jason


REST API: Display Category names in JSON?

1  Asked on August 3, 2020 by steve


Gutenberg withInstanceId. When to use it?

2  Asked on July 25, 2020 by at-least-three-characters


Ask a Question

Get help from others!

© 2021 All rights reserved.