Bird
0
0

How would you write a tax_query to find posts that belong to either the 'news' category or have the tag 'featured', using the correct relation?

hard📝 Application Q9 of 15
Wordpress - WordPress Query and Database
How would you write a tax_query to find posts that belong to either the 'news' category or have the tag 'featured', using the correct relation?
A'tax_query' => [ 'relation' => 'NOT', [ 'taxonomy' => 'category', 'field' => 'slug', 'terms' => ['news'] ], [ 'taxonomy' => 'post_tag', 'field' => 'slug', 'terms' => ['featured'] ] ]
B'tax_query' => [ 'relation' => 'AND', [ 'taxonomy' => 'category', 'field' => 'slug', 'terms' => ['news'] ], [ 'taxonomy' => 'post_tag', 'field' => 'slug', 'terms' => ['featured'] ] ]
C'tax_query' => [ [ 'taxonomy' => 'category', 'field' => 'slug', 'terms' => ['news', 'featured'] ] ]
D'tax_query' => [ 'relation' => 'OR', [ 'taxonomy' => 'category', 'field' => 'slug', 'terms' => ['news'] ], [ 'taxonomy' => 'post_tag', 'field' => 'slug', 'terms' => ['featured'] ] ]
Step-by-Step Solution
Solution:
  1. Step 1: Understand relation 'OR'

    To get posts matching either condition, use 'relation' => 'OR'.
  2. Step 2: Check tax_query arrays

    Separate arrays for category 'news' and tag 'featured' are correct.
  3. Final Answer:

    tax_query with 'relation' => 'OR' and separate arrays for category and tag -> Option D
  4. Quick Check:

    Use 'OR' relation for either/or tax_query filters [OK]
Quick Trick: 'OR' relation returns posts matching any taxonomy condition [OK]
Common Mistakes:
  • Using 'AND' instead of 'OR' for either/or queries
  • Combining terms incorrectly in one array
  • Using invalid relation 'NOT'

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Wordpress Quizzes