AgentWP

  • Tutorials
  • Themes
  • Plugins
  • Services
  • Contact

Code To Show Total Number of Published Posts on WordPress Site

Do you want to find and display total number of posts published on your WordPress site? Here’s the code.

wordpress code

Get the number of published posts using the $wpdb object, while making sure to exclude the post revisions and auto saved posts.

$agentwp_num = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' AND post_name NOT LIKE '%revision%' AND post_name NOT LIKE '%autosave%'" );

Now, the $agentwp_num variable contains the total number of posts published on your WordPress blog, excluding the auto saved posts and post revisions.

It can be displayed anywhere in the template files with the code below,

if (0 < $agentwp_num) $agentwp_num = number_format($agentwp_num);
echo $agentwp_num.' posts published since we are online!';

Get useful WordPress related tricks, tips, tutorials and reviews delivered straight to your inbox.

StudioPress - Premium Wordpress Themes
ElegantThemes - Premium Wordpress Themes

Copyright © 2023 · AGENTWP · All Rights Reserved

Copyright © 2023 · awp on Genesis Framework · WordPress · Log in