Support » Plugins » Count the number of posts of only one category

  • Hello,

    i want to see the number of posts of a category, but:

    – i don’t want to use wp_list_categories
    – i have categories under the category, and i want count all of the posts
    – i don’t know how to make a mysql query
    – i have test but i have a wrong number 🙁

    i just want to make: “[number] posts in this category”

    help me please 🙂

    thank you,

Viewing 15 replies - 1 through 15 (of 15 total)
  • Didn’t test this but maybe this for Category ID=14

    $post_count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id=14;");?>
    <p><?php echo 'post count is ' . $post_count; ?></p>

    it’s weird;

    with the plugin, i have 5 for result
    with the plugin and your code it’s 20
    but i have 104 posts in the category :/

    Darn, forget that as it will include revisions. Try this:

    $mycats=get_categories ('include=14');
    echo 'my cat count is '. $mycats[0]->category_count;

    i try this:

    $mycats=get_categories ('include=14');
    echo 'my cat count is '. $mycats[0]->category_count;
    ?> posts in this category.

    but i have only the echo text, and no number 🙁


    Make sure you are using the correct category ID.

    Hover over the Category Name in your Posts->Categories panel to ascertain the category ID.

    Oh, i see,

    this is my organization:

    —- cat 1
    ———cat 1.1
    ———cat 1.2
    —- cat 2
    ———cat 2.1
    ———cat 2.2

    so, it’s not possible to count the total number of posts directly on category_principal?

    so, how to have many cat in “WHERE term_taxonomy_id=1” ?

    bye 🙂

    I try:

    $post_count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id=14
    AND WHERE term_taxonomy_id=1
    AND WHERE term_taxonomy_id=2
    AND WHERE term_taxonomy_id=3;");?>
    <p><?php echo 'post count is ' . $post_count; ?></p>

    but no result 🙁 is not the good syntax?


    Guess using my code above you could add the exclude parameter:

    $mycats=get_categories ('include=14&exclude=5,7');

    where 14 if the category id of principal and where 5 and 7 are the category ids of the cat 1.1 and cat 1.2.

    sorry, doesn’t work, but i’m sure that is the correct id 🙁

    So if you use wp_list_categories with those same parameters do you get the categories you want?


    <?php wp_list_categories('orderby=name&show_count=1&include=97&use_desc_for_title=0'); ?>

    i have the error:

    Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/blog/wp-includes/taxonomy.php on line 1965

    Warning: join() [function.join]: Invalid arguments passed in /home/blog/wp-includes/taxonomy.php on line 1965

    Warning: Invalid argument supplied for foreach() in /home/blog/wp-includes/taxonomy.php on line 1972

    Well that wp_list_categories works for me on a 2.7 blog but guessing you are using 2.6.5 (or a version close to that).

    Could be something wrong your with database tables. Might try repairing you database tables, but backup your database before doing that.

    i upgrade to wp 2.7 and i have no errors 🙂 but it’s only possible to calculate the number of posts of only one sub-category (example the cat 1.1) and not the principal category (example cat 1) 🙁



    already no solution for calculate number of posts of a general category and the sub-category, in a same only one result? 🙁


    nobody? 🙁

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Count the number of posts of only one category’ is closed to new replies.