0
0
Wordpressframework~20 mins

Tax queries and meta queries in Wordpress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Tax and Meta Query Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What posts are returned by this tax query?
Consider this WordPress query snippet. Which posts will it return based on the tax query?
Wordpress
<?php
$args = [
  'post_type' => 'product',
  'tax_query' => [
    [
      'taxonomy' => 'product_cat',
      'field' => 'slug',
      'terms' => ['electronics', 'appliances'],
      'operator' => 'AND'
    ]
  ]
];
$query = new WP_Query($args);
?>
APosts in 'electronics' category only
BPosts in either 'electronics' OR 'appliances' categories
CPosts in 'appliances' category only
DPosts in both 'electronics' AND 'appliances' categories
Attempts:
2 left
💡 Hint
The 'operator' key controls how terms are combined in tax queries.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this meta query
Which option correctly fixes the syntax error in this meta query array?
Wordpress
<?php
$args = [
  'post_type' => 'post',
  'meta_query' => [
    'relation' => 'OR',
    [
      'key' => 'price',
      'value' => 100,
      'compare' => '>'
    ],
    [
      'key' => 'stock',
      'value' => 0,
      'compare' => '>='
    ]
  ]
];
$query = new WP_Query($args);
?>
AAdd a comma after 'relation' => 'OR' to separate it from the first array
BChange 'compare' => '>' to 'compare' => '>' (no change needed, code is correct)
CWrap the meta_query arrays inside another array to fix nesting
DReplace 'relation' => 'OR' with 'relation' => 'AND'
Attempts:
2 left
💡 Hint
Check array syntax carefully, especially commas between elements.
state_output
advanced
2:00remaining
What is the value of $count after this meta query?
Given this code, what is the value of $count after running the query?
Wordpress
<?php
$args = [
  'post_type' => 'event',
  'meta_query' => [
    [
      'key' => 'event_date',
      'value' => date('Y-m-d'),
      'compare' => '>=',
      'type' => 'DATE'
    ]
  ]
];
$query = new WP_Query($args);
$count = $query->found_posts;
?>
AZero, because 'found_posts' is not set for meta queries
BNumber of 'event' posts with 'event_date' today or in the future
CNumber of 'event' posts with 'event_date' in the past
DNumber of all 'event' posts regardless of date
Attempts:
2 left
💡 Hint
The meta query filters posts by date compared to today.
🔧 Debug
advanced
2:00remaining
Why does this tax query return no posts?
This tax query returns no posts even though posts exist in the 'news' category. What is the cause?
Wordpress
<?php
$args = [
  'post_type' => 'post',
  'tax_query' => [
    [
      'taxonomy' => 'category',
      'field' => 'name',
      'terms' => 'news'
    ]
  ]
];
$query = new WP_Query($args);
?>
A'taxonomy' should be 'post_category' instead of 'category'
B'field' should be 'slug' instead of 'name'
C'terms' should be an array, not a string
DMissing 'operator' key causes no results
Attempts:
2 left
💡 Hint
Check the data type of 'terms' in tax queries.
🧠 Conceptual
expert
2:00remaining
How does combining tax_query and meta_query affect results?
If a WP_Query uses both 'tax_query' and 'meta_query' arrays, how are the results filtered?
APosts must match BOTH tax_query AND meta_query conditions
BPosts matching EITHER tax_query OR meta_query conditions are returned
COnly tax_query is applied; meta_query is ignored
DOnly meta_query is applied; tax_query is ignored
Attempts:
2 left
💡 Hint
Think about how multiple query parameters combine in WP_Query.