Skip to content

Instantly share code, notes, and snippets.

@DWboutin
Created July 2, 2014 14:26
Embed
What would you like to do?
Custom taxonomy post count
<?php
function catCount($slug){
global $wpdb;
$query = "
SELECT COUNT( DISTINCT cat_posts.ID ) AS post_count
FROM gh_term_taxonomy AS cat_term_taxonomy INNER JOIN gh_terms AS cat_terms ON
cat_term_taxonomy.term_id = cat_terms.term_id
INNER JOIN gh_term_relationships AS cat_term_relationships
ON cat_term_taxonomy.term_taxonomy_id = cat_term_relationships.term_taxonomy_id
INNER JOIN gh_posts AS cat_posts
ON cat_term_relationships.object_id = cat_posts.ID
WHERE cat_posts.post_status = 'publish'
AND cat_posts.post_type = 'product'
AND cat_term_taxonomy.taxonomy = 'product_cat'
AND cat_terms.slug = ('$slug')
";
return $wpdb->get_var($query);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment