SQL Count Posts in Category


function count_posts($cat_id){
global $wpdb;

$query = "
SELECT COUNT(*) FROM $wpdb->posts
LEFT JOIN $wpdb->term_relationships ON
($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON
($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE $wpdb->posts.post_status = 'private'
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->term_taxonomy.term_id = $cat_id
return $wpdb->get_var($query);



You must be logged in to post a comment.

  1. Whats Taking place i'm new to this, I stumbled upon this I've found It absolutely helpful and it has aided me out loads. I'm hoping to contribute & aid other customers like its aided me. Good job.

  2. I really appreciate this post. I¡¦ve been looking all over for this! Thank goodness I found it on Bing. You've made my day! Thx again

  3. <link rel="stylesheet" href="css/handheld.css" type="text/css" media="handheld" />

    Try this, you can have a specific stylesheet and edit said css to make your site look better in a mobile device.

  4. Hello! Quick question that's completely off topic. Do you know how to make your site mobile friendly? My site looks weird when viewing from my iphone 4. I'm trying to find a template or plugin that might be able to resolve this issue. If you have any suggestions, please share. Many thanks!