WordPress.org

Ready to get started?Download WordPress

Forums

How to exclude categories (2 posts)

  1. eddyj
    Member
    Posted 3 years ago #

    Ok i have this code which displays total number of POSTS ( not pages ) published

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

    Now its counting all posts within all categories.

    Is there a way to exclude it counting posts within a category name?

    Like I have 10 categories on my blog its counting all posts in all 10 categories, but i want it to only count posts in 9 categories, so best way i can think is to code it so that it excludes posts in the 10th category...

    So the above code is just for displaying count number of all posts, any ideas?

  2. Dan Cole
    Member
    Posted 3 years ago #

    I'm not sure how to exclude, but you can include categories by adding a SQL statement like:

    AND $wpdb->post2cat.category_id IN (1,2,3,4,5,6,7,8,9)

    which would make your code look like:

    <?php echo $numposts = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' AND $wpdb->post2cat.category_id IN (1,2,3,4,5,6,7,8,9)");
    if (0 < $numposts) $numposts = number_format($numposts); ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.