0

I am trying to create a shortcode to display the posts count of a custom post type as well as standard post categories. I have successfully done this using this code snippet from a similar post:

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

The code above only works for standard wordpress post categories, not custom post types. Any help is appreciated.

0

It's because you are have passed "category" in "get_terms_by()" function as third argument.

Please replace "add_custom_taxonomy_here" with your custom taxonomy slug in below code.

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

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

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.