• I used to have a plugin that could count posts by category, so I used it to count the number of recipes on my site (http://www.mattfischer.com/ramen). WP 2.3 broke the plugin with the new schema, that I frankly do not understand.

    So using the Post Count plugin, I tried to get it work work like this:

    SELECT COUNT(*) FROM $wpdb->posts WHERE post_category = 2 ….

    The issue now seems to be that Category 2 is a “parent” category and this query didn’t include the sub-cats. Is there any easy fix to this without saying OR category = 3 OR category = 4….?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter mfischer2

    (@mfischer2)

    Well, I have a solution, but it’s probably not the best one:

    SELECT count FROM $wpdb->term_taxonomy WHERE term_id = 2

    Thanks mfischer2! This also works:

    function counting() {
    		global $wpdb;
    		echo $wpdb->get_var("SELECT count FROM term_taxonomy WHERE term_taxonomy_id = 'your_category_id'");
    		}

    But must confess I am a bit lost on how to only count posts that were published.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘count by category’ is closed to new replies.