Count posts in custom taxonomy

Home Forums Count posts in custom taxonomy

This topic contains 0 replies, has 1 voice, and was last updated by  John 2 years, 2 months ago.

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #8916

    John
    Participant

    Is there a way to count all published posts from a custom taxonomy?

    While looking around I found this snippet but I didn’t manage to get it to work …

    global $wpdb;
    $query = "
            SELECT COUNT( DISTINCT cat_posts.ID ) AS post_count
            FROM wp_term_taxonomy AS cat_term_taxonomy INNER JOIN wp_terms AS cat_terms ON
            cat_term_taxonomy.term_id = cat_terms.term_id
            INNER JOIN wp_term_relationships AS cat_term_relationships 
            ON cat_term_taxonomy.term_taxonomy_id = cat_term_relationships.term_taxonomy_id
            INNER JOIN wp_posts AS cat_posts 
            ON cat_term_relationships.object_id = cat_posts.ID
            WHERE cat_posts.post_status = 'publish' 
            AND cat_posts.post_type = 'post' 
            AND cat_term_taxonomy.taxonomy = 'YOUR-CUSTOM-TAXONOMY' 
            AND cat_terms.slug IN ('TERM-SLUG-1, TERM-SLUG-2')
        ";
    return $wpdb->get_var($query);
    
Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.

Skip to toolbar