(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'.

And here is that class which you can copy into your theme's functions.php file or use in the .php file of a plugin you might be writing. It is probably overkill (and probably overly complex) for your needs but in case you want a hybrid solution it should give you want you want and if you have lots of posts (hundreds?) it will perform better than loading all posts into an array just to be able to count them:

A couple things to explain. WP_Query() doesn't let us query for a list of taxonomy term IDs, but it does let us query for a list of category IDs. But what are categories? They are a a taxonomy named 'category'? That's why I set up WP_Query() to query by category IDs using your taxonomy term IDs, but then I do a string replace in the posts_where hook to replace 'category' with '{$taxonomy}', or in your case with 'artworkcat'.

All the apparent complexity around the add_action() and remove_action() is simply to allow you to only use those hooks when you need them and not to leave them laying around where they could potentially affect your other queries.

Sorich's code is great if you're comfortable manipulating SQL queries. If you'd rather build a simple query, you can use get_posts() to return an array of post objects that match the desired query parameters and count them at the same time, returning a value. I've used this to count all sorts of combinations, as it's easy to modify.