Combine “AND” with “OR” for Category Relation (advanced)

Let’s say you have two main categories for your posts:

  • Europe
  • Asia

 

And posts from these main categories also have one or more of the same additional categories.

  • Europe
    • Flights
    • Hotels
    • Dining
  • Asia
    • Flights
    • Hotels
    • Dining

 

Now lets say you only want to include posts that have “Europe” as a category, but also only want to include “Europe” posts with either “Flights” or “Hotels”, and NOT “Dining”.

So the logic would be:

(Europe AND Flights) OR (Europe AND Hotels)

This can be accomplished by hooking into one of the plugin’s PHP filters.

1. For the above scenario, choose "Europe", "Flights" and "Hotels" as your Categories, and then choose "OR" for the Category Relation.

2. Next, add the following PHP code to your theme's "functions.php" file.

In the following code, the number “121” is the Category ID for “Europe”.  Adjust this number accordingly.

Variation of above code for WooCommerce Product Categories.

Variation to include Sub Categories