Skip to content

Instantly share code, notes, and snippets.

@NeilJS
Created March 20, 2012 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NeilJS/2137474 to your computer and use it in GitHub Desktop.
Save NeilJS/2137474 to your computer and use it in GitHub Desktop.
WP - Get total number of posts in Custom Type Posts
// Put in functions.php
/* GET TOTAL NUMBER OF POSTS FOR SPECIFIC CUSTOM POST TYPE */
function cpt_count($ptype,$display=true) {
global $wpdb;
$SQL = "SELECT count(*) FROM $wpdb->posts posts WHERE posts.post_status = 'publish' AND post_type = '".$ptype."' ";
$total_cpt_posts = $wpdb->get_var($SQL);
if($display) {
echo $total_cpt_posts;
}
return $total_cpt_posts;
}
// Usage
<?php cpt_count('case-study'); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment