Aller au contenu
FR

Count posts in a specific WordPress taxonomy

Version audio de cet article

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