SQL query to retrieve the number of WordPress posts with all given categories
There is a working example that only counts posts that are associated with both categories. This is tested with 3 posts, not 5000. function count_posts_with_categories($cat_ids) { global $wpdb; $cat_count = 0; // A subquery to get all posts that are assigned to each category $subquery = “SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id IN (” . … Read more