How to Create a Shortcode in WordPress to Count All Posts with Some Categories Excluded

If you’re looking for a way to count all posts on your WordPress site while excluding posts in specific categories, a shortcode is a great solution. By creating a shortcode, you can easily display the post count wherever you want on your site, such as in a page, a post or widget and navigation menu.

In this tutorial, we’ll show you how to create a shortcode in WordPress to count all posts with some categories excluded.

Before proceeding with any customizations in WordPress, it’s essential to set up a child theme. A child theme acts as a safe and efficient way to make modifications without affecting the parent theme. If you haven’t set up a child theme yet, follow this tutorial on How to Create a Child Theme for Customization. It will guide you through the process and ensure that your customizations remain intact even after theme updates.

Step 1: Create and Register the Shortcode Function

To create the shortcode function, you can add the following code to your theme’s functions.php file or a plugin file:

function exclude_category_count_func( $atts ) {
	$atts = shortcode_atts(
			'exclude_cats' => '',

	$exclude_cats = explode( ',', $atts['exclude_cats'] );

	$args = array(
		'category__not_in' => $exclude_cats,
		'posts_per_page'   => -1,
		'fields'           => 'ids',

	$post_ids   = get_posts( $args );
	$post_count = count( $post_ids );

	return $post_count;
add_shortcode( 'exclude_category_count', 'exclude_category_count_func' );
Code language: PHP (php)

This function uses the shortcode_atts() function to set default values for the shortcode attribute exclude_cats. It then converts the attribute to an array using explode(). The get_posts() function is used to retrieve posts that don’t belong to the excluded categories, and count() is used to get the number of posts. Finally, the add_shortcode() function is used to register a new shortcode named exclude_category_count. The second parameter is the name of the function that will be used to generate the shortcode output.

Step 2: Use the Shortcode

Once you have registered the shortcode, you can use it on any post or page in your WordPress site. Here’s an example usage of the shortcode:

[exclude_category_count exclude_cats="1,2"]
Code language: PHP (php)

This shortcode will count all posts in the WordPress site except those belonging to the categories with IDs 1 and 2.

That’s it! Creating a shortcode to count all posts with some categories excluded is a powerful technique to have in your WordPress toolbox. By following the steps in this tutorial, you can create a shortcode that meets your specific requirements and easily display the post count wherever you want on your site.

Leave your feedback and help us improve ๐Ÿถ

We hope you found this article helpful! If you have any questions, feedback, or spot any errors, please let us know in the comments. Your input is valuable and helps us improve. If you liked this article, please consider sharing it with others. And if you really enjoyed it, you can show your support by buying us a cup of coffee โ˜•๏ธ or donating via PayPal ๐Ÿ’ฐ.

More free knowledge, because why not?

Your thoughts matter, leave a reply ๐Ÿ’ฌ

Your email address will not be published. Required fields are marked *