NB: Make sure you do a back up of your theme, files and database before attempting the tutorials

Intermediate
Web Hosting Canada

Last modified : Sep 03 2022

Estimated reading time : 0 minutes, 58 seconds - 111 words

Count posts in a specific WordPress taxonomy: This tutorial will show you how to create a query that will let you display the number of posts linked to a specific taxonomy slug.

In our example below, our taxonomy is called video-catégorie that you need to force upon your arguments. We created a new category called En discothèque with the en-discotheque slug (a category or taxonomy slug is the ID given when we create it).

Here’s the code snippet to add into your single.php template:

<?php 
////// Slug ou identifiant du non de la catégorie classée dans votre taxonomie /////
$catpagediscotheque = 'en-discotheque';
///////////////////////////////////// Requête //////////////////////////////////////
$args = array(
'post_type' => 'viavideos',
'post_status' => 'published',
/// Ici on met notre taxonomy name ////
'video-categorie' => $catpagediscotheque,
'numberposts' => -1
);
echo $num = count( get_posts( $args ) );
?>

You will get the number of posts linked to your taxonomy’s slug.
Understanding taxonomiesSave a taxonomy

Leave a Reply

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

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>