1

I have some checkboxes that relate to categories, and when checked and a search is made, it returns any posts in any of the checked categories.

This is my query:

    $query = new WP_Query([
    'post_type' => ['event', 'video'],
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'tax_query' => array(
      'relation' => 'OR',
      array(
        'taxonomy' => 'video_category',
        'field' => 'term_id',
        'terms' => $category,
        'operator' => 'in',
      )
    ),
    's' => $_POST['find'],
  ]
);

This works fine, except that I have a few categories which, when checked, should get any posts that have that category, and any of the other categories not in the three.

For example, say I have the following categories:

  • Desktop
  • Mobile
  • Red
  • Green
  • Blue
  • Cyan
  • Magenta
  • Yellow
  • Purple
  • Black

If I check any of the colors, I'd like to return any posts that have any of those colors as categories. But if I check 'Desktop' and any of the colors, I'd like to return only posts that have the 'Desktop' category, but also any of the color categories (eg. 'Desktop red', 'Desktop black yellow').

Any help in how I could set up the categories, or the query, to do this would be much appreciated.

Your Answer

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

Browse other questions tagged or ask your own question.