(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 !
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:
just replace genere with your taxonomy slug and romantic with the specific term.
wp_count_posts()
will not work for this, it does not accept a taxonomy parameter. You can do the following:To get a list of terms inside a taxonomy, use
get_terms()
.E.g.:
get_terms('artworkcat')
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.Change the post_status arg to “publish” if you only want to count published items.
And if you have the Query Multiple Taxonomies plugin by scribu installed, you can add multiple taxonomies to the args…
@somatic’s answer is probably the best for most sites; it is certainly the simplest. And if you are very comfortable with SQL you can use @sorich87’s answer. Or you can use my hybrid solution below.
There was a very similar question asked last week (definitely read the answer to that question before reading the answer I left for you here):
Taking the answer from that and modifying it slightly you get a call that look like this:
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 upWP_Query()
to query by category IDs using your taxonomy term IDs, but then I do a string replace in theposts_where
hook to replace'category'
with'{$taxonomy}'
, or in your case with'artworkcat'
.All the apparent complexity around the
add_action()
andremove_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.If your taxonomy only exists for the specific post type just use the core wp_count_terms function.
Hello,
I believe that the simple and most efficient way to
count posts
in custom post type or in a taxonomy is:Then to echo it as follow:
Hope this help.