WordPress Shortcode to get Total Number of Posts

Here’s a shortcode to get the total number of published posts in your WordPress website.

Shortcode: [total_posts]

Code for the shortcode:

//To get total posts
function sw_total_posts() { 
$total = wp_count_posts()->publish;
return $total; 
} 
add_shortcode('total_posts','sw_total_posts');
PHP

Wherever you want to display the total number of posts, you can simply use the [total_posts] shortcode.

How to add this code to your WordPress site

Leave a Comment