0

I'm trying to show posts from categories A, B, C, D...and Y, except X, unless posts in X are also in Y, then show them with the rest.

I can hide category X on it's own 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 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 other categories) and using it with a category parameter to exclude posts in X...somehow, 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!

0

In your wp_query, you should use NOT IN which I don't see. In your arguments for WP_Query(), you should use category__not_in instead of cat. So, change your code to:

Excluding Single Category

$args = array(
  'post_type' => 'post',
  'posts_per_page' => 15,
  'paged' => $paged,
  'category__not_in' => 'X', // string
);

If you wish to exclude multiple categories then assign the category ID's comma separated in an array, not as a string. Check the below code

Excluding Multiple Categories and Displaying Posts That are from X and Y

$args = array(
  'post_type' => 'post',
  'posts_per_page' => 15,
  'paged' => $paged,
  'category__in' => array(X,Y), // Array
  'post__not_in' => array(X),
);

Below is the full Query:

$args = array(
  'post_type' => 'post',
  'posts_per_page' => 15,
  'paged' => $paged,
  'category__in' => array(X,Y), // Array
  'post__not_in' => array(X),
);

$your_query = new WP_Query( $args );
  while( $your_query->have_posts() ):
  $your_query->the_post();
  the_title();
endwhile;

wp_reset_postdata();

Note: Use ID not the category slug or name

  • According to the Wordpress Codex, cat => '-285' works fine to exclude categories, and I can confirm it does because I've tested it. This isn't the problem though if you read through my entire post. I need to not only exclude category X, but also allow it if it is paired with category Y. – dmoz Dec 1 '18 at 6:38
  • For that, you don't need to combine them and merge. I have updated the code. That pulls the categories from combined from both categories. – Maqk Dec 1 '18 at 8:33
  • I wound grab all the posts and filter later with a foreach unsetting the ones belonging ONLY to cat X. I dont't see a straight solution with just a wp_query – Andrea Somovigo Dec 1 '18 at 8:35
  • I think the 'category__in' => array(X,Y), 'post__not_in' => array(X), still will not show what I need. I believe that's going to only get posts in X and Y, and then exclude posts in X, leaving us with only posts in Y. Right? Maybe I should've made it more clear(I edited my OP) that I need all posts from every other category—so posts from categories A, B, C, D...and Y, except X, unless posts in X are also in Y. Let me know if that doesn't make sense or is unclear. I realize it's a bit of an odd query. – dmoz Dec 2 '18 at 23:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.