(Moderator’s note: The original title was “How to know the number of custom type posts having a specific taxonomy”)

I’m stuck with the function wp_count_posts()!

I created custom post types called 'artwork' to display artworks in a gallery. I also created a custom taxonomy called 'artworkcat' to sort each artwork into a specific category ('webdesign', 'logo', 'print').

I would like to use the wp_count_posts() to know how many posts I have using the 'artwork' custom post type, in a specific category.

If that’s not clear, I give you a practical example : I would like to know how many posts I have in this 'artwork' custom post type, with the ‘artworkcat’ taxonomy called ‘webdesign’.

Does wp_count_posts() work for this, adding some parameters ?

Thanks for the help !

6 Answers
6

Somatic had the cleanest answer, but missed one thing. You should specify the numberposts to be -1 so that it counts them all. Like this:

$args = array(
    'post_type' => 'artwork',
    'post_status' => 'published',
    'genre' => 'romantic',
    'numberposts' => -1
);
$num = count( get_posts( $args ) );

just replace genere with your taxonomy slug and romantic with the specific term.

Leave a Reply

Your email address will not be published. Required fields are marked *