Custom $wpdb Query for Custom Post Type by Category
Don’t use $wpdb, use wp_query. Not only will your code be leaner and quicker, you’ll avoid issues like this. Unless you’re doing something with custom tables or seriously in-depth SQL, there’s no reason to do direct SQL calls on $wpdb. So, to answer your question – rewrite it from the ground up using wp_query, see … Read more