Forums

[resolved] Number of posts from selected category (4 posts)

  1. amistad18
    Member
    Posted 3 years ago #

    Hi,

    I can get the number of all post on the site, when I used that code:

    <?php
    $post_number = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish'");
    if (0 < $post_number) $post_number = number_format($post_number);
    ?>

    <?php echo "$post_number"; ?>

    What code I have to use, if I want to get the number of posts in selected category? Selected by name od ID of some category ...

    Any help will be greatly appreciated.
    Best regards, amistad18.

  2. MichaelH
    Volunteer
    Posted 3 years ago #

    Get count for category id 17:

    <?php
    $cats=get_categories('include=17');
    foreach ( (array) $cats as $cat ) {
    echo 'id = ' . $cat->cat_ID . ' name =' . $cat->name . ' count = ' .$cat->count ;
    }
    ?>
  3. MichaelH
    Volunteer
    Posted 3 years ago #

    Oops forgot to mention this easier method of counting all published posts:

    <?php
    $numposts = wp_count_posts();
    echo 'number of posts is ' . $numposts->publish;
    ?>
  4. amistad18
    Member
    Posted 3 years ago #

    Thank you very much - that code is working ;)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.