WordPress.org

Ready to get started?Download WordPress

Forums

Count the number of posts of only one category (16 posts)

  1. csseur3
    Member
    Posted 5 years ago #

    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 http://wordpress.org/extend/plugins/count-posts/ but i have a wrong number :(

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

    help me please :)

    thank you,
    bye

  2. MichaelH
    Member
    Posted 5 years ago #

    Didn't test this but maybe this for Category ID=14

    <?php
    $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>
  3. csseur3
    Member
    Posted 5 years ago #

    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 :/

  4. MichaelH
    Member
    Posted 5 years ago #

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

    <?php
    $mycats=get_categories ('include=14');
    echo 'my cat count is '. $mycats[0]->category_count;
    ?>
  5. csseur3
    Member
    Posted 5 years ago #

    i try this:

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

    but i have only the echo text, and no number :(

    bye

  6. MichaelH
    Member
    Posted 5 years ago #

    Make sure you are using the correct category ID.

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

  7. csseur3
    Member
    Posted 5 years ago #

    Oh, i see,

    this is my organization:

    category_principal
    ---- 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 :)

  8. csseur3
    Member
    Posted 5 years ago #

    I try:

    <?php
    $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?

    bye

  9. MichaelH
    Member
    Posted 5 years ago #

    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.

  10. csseur3
    Member
    Posted 5 years ago #

    sorry, doesn't work, but i'm sure that is the correct id :(

  11. MichaelH
    Member
    Posted 5 years ago #

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

  12. csseur3
    Member
    Posted 5 years ago #

    with:

    <?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

  13. MichaelH
    Member
    Posted 5 years ago #

    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.

  14. csseur3
    Member
    Posted 5 years ago #

    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) :(

    bye

  15. csseur3
    Member
    Posted 5 years ago #

    re,

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

    bye.

  16. csseur3
    Member
    Posted 5 years ago #

    nobody? :(

Topic Closed

This topic has been closed to new replies.

About this Topic