How do I exclude all posts from a specific category in a $wpdb query?
-
I’m trying to create a function for my statistics page that will count all published posts in a given year that are NOT assigned to category ID ‘667’. Here is what I have so far:
function my_post_count_function( $year ) { global $wpdb; return $wpdb->get_var( "SELECT count(ID) FROM $wpdb->posts WHERE $wpdb->posts.post_type IN ('post') AND $wpdb->posts.post_status IN ('publish') AND YEAR($wpdb->posts.post_date) = $year" ); }
The function works, but I need help figuring out how to exclude the category. I believe I need to add a JOIN, but I’ve never joined tables in a query before. I’m unsure of how to begin and haven’t had much success in finding examples.
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘How do I exclude all posts from a specific category in a $wpdb query?’ is closed to new replies.