I’m trying to exclude one category (let’s call it X) from a general WP_Query, while also allowing posts in category X only if it is in category X and Y.
I can hide category X pretty simply:
$ args = array( 'post_type' => 'post', 'posts_per_page' => 15, 'paged' => $ paged, 'cat' => '-X', );
I’ve tried to combine two loops, one getting all posts excluding those in category X, the other getting posts that are in both X and Y, but it doesn’t seem to work:
$ EverythingButXLoop_args = array( 'post_type' => 'post', 'cat' => '-X', ); $ EverythingButXLoop = new WP_Query( $ EverythingButXLoop_args ); $ OnlyXwithYLoop_args = array( 'post_type' => 'post', 'category_name' => 'categoryX+categoryY', ); $ OnlyXwithYLoop = new WP_Query( $ OnlyXwithYLoop_args ); $ mainLoop_args = array( 'posts_per_page' => 15, 'paged' => $ paged, ); $ mainLoop = new WP_Query( $ mainLoop_args ); $ mainLoop->posts = array_merge( $ EverythingButXLoop->posts, $ OnlyXwithYLoop->posts ); //populate post_count count for the loop to work correctly $ mainLoop->post_count = $ EverythingButXLoop->post_count + $ OnlyXwithYLoop->post_count;
I’ve also thought about getting all category ID’s in a var(instead of listing every one of my many categories) and using it in a category parameter of WP_Query, ie.
'cat' => $ categoryIDs, 'category__not_in' => 'X' but I think I’m still just faced with the original problem: excluding X, except when in X and Y
Any idea what’s not working with the combining technique above, or how I can achieve this another (better) way? Thanks in advance!