If you run a WooCommerce store, you may want to display the total number of products on a page, post, or custom section of your website. For example, you could show a message such as “We have 250 products available” to visitors.
You can do this by creating a custom WordPress shortcode using PHP. WordPress provides the wp_count_posts() function to retrieve the number of posts for a specific post type, including WooCommerce products.
In this tutorial, you will learn how to create shortcodes to display the number of published products and the total number of products across different post statuses.
1. Create a Shortcode to Display the Number of Published Products
WooCommerce products use the WordPress post type product. You can use wp_count_posts() to retrieve the product counts and access the publish property to get the number of published products.
Add the following code to your active child theme’s functions.php file or a custom plugin:
/**
* Display the number of published WooCommerce products.
*/
function geekerhub_total_published_products_shortcode() {
$product_counts = wp_count_posts( 'product' );
if ( ! $product_counts ) {
return '0';
}
return (string) $product_counts->publish;
}
add_shortcode(
'total_published_products',
'geekerhub_total_published_products_shortcode'
);
This code registers a shortcode named total_published_products. It returns the number of products with the published status.
Use the following shortcode in a page or post:
[total_published_products]
WordPress will replace the shortcode with the published product count when the page is displayed.
2. Create a Shortcode to Display Products Across All Non-Trash Statuses
If you want to count products that are not published yet, you can add the counts for the different product statuses. For example, this can include published, draft, pending, future, and private products.
Use the following code:
/**
* Display the number of products across non-trash statuses.
*/
function geekerhub_total_products_shortcode() {
$product_counts = wp_count_posts( 'product' );
if ( ! $product_counts ) {
return '0';
}
$total_products = 0;
foreach ( get_object_vars( $product_counts ) as $status => $count ) {
if ( 'trash' === $status ) {
continue;
}
$total_products += (int) $count;
}
return (string) $total_products;
}
add_shortcode(
'total_products',
'geekerhub_total_products_shortcode'
);
This shortcode adds together the counts for all returned product statuses except the trash status.
Use it in your content like this:
[total_products]
The shortcode will display the resulting product count.
3. Display the Product Count with a Custom Message
You can also return a message along with the product count. This is useful when you want to display the number in a promotional section or store information area.
For example, update the published products shortcode as follows:
/**
* Display published product count with a message.
*/
function geekerhub_product_count_message_shortcode() {
$product_counts = wp_count_posts( 'product' );
$total_products = $product_counts
? (int) $product_counts->publish
: 0;
return sprintf(
'We currently have %s products available.',
number_format_i18n( $total_products )
);
}
add_shortcode(
'product_count_message',
'geekerhub_product_count_message_shortcode'
);
Now add this shortcode to a page or post:
[product_count_message]
It will display a message similar to:
We currently have 250 products available.
The number will change automatically as the published product count changes.
Where Should You Add the Shortcode Code?
You can add these PHP functions to your active theme’s functions.php file. However, using a small custom plugin is a better option if you want the shortcode to remain available when you change your WordPress theme.
If you need help creating one, read our guide on how to create a custom plugin in WordPress.
For more details about registering shortcodes, see the official WordPress documentation for add_shortcode() and the wp_count_posts() function.
Important Notes
- Published products: The
total_published_productsshortcode counts only products with the published status. - Other statuses: The
total_productsshortcode adds the counts returned by WordPress for product statuses other than trash. - Shortcode placement: You can use these shortcodes in WordPress posts and pages, or in areas that support shortcode execution.
- WooCommerce requirement: The examples are intended for a WooCommerce store where the
productpost type is registered. - Performance: These examples use WordPress post-count data rather than loading every product individually.
Conclusion
You can display the number of WooCommerce products anywhere on your WordPress website by creating a custom shortcode. Use wp_count_posts( 'product' ) to retrieve product counts and register your shortcode with add_shortcode().
Choose the published product count if you want to show visitors how many products are available. If you need to include products in other statuses, use the second example and exclude the trash count.