Challenge - 5 Problems
Custom Query Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
How does WP_Query filter posts by category?
Given a WP_Query with 'category_name' set, what posts will it retrieve?
Wordpress
<?php $query = new WP_Query(['category_name' => 'news']); while ($query->have_posts()) { $query->the_post(); the_title(); } ?>
Attempts:
2 left
💡 Hint
Think about how 'category_name' parameter works in WP_Query.
✗ Incorrect
The 'category_name' parameter filters posts strictly by the exact category slug. So only posts assigned to the 'news' category are retrieved.
📝 Syntax
intermediate2:00remaining
Correct syntax for meta_query in WP_Query
Which option correctly retrieves posts where meta key 'color' equals 'blue'?
Wordpress
<?php
$args = [
'meta_query' => [
// Fill here
]
];
$query = new WP_Query($args);
?>Attempts:
2 left
💡 Hint
meta_query expects an array of arrays with keys 'key', 'value', and 'compare'.
✗ Incorrect
The correct syntax uses 'key' and 'value' inside an array inside 'meta_query'. Option B matches this pattern exactly.
🔧 Debug
advanced2:00remaining
Why does this custom query return no posts?
This WP_Query returns no posts. What is the cause?
Wordpress
<?php $args = [ 'post_type' => 'product', 'category_name' => 'electronics' ]; $query = new WP_Query($args); ?>
Attempts:
2 left
💡 Hint
Think about how categories relate to custom post types.
✗ Incorrect
Categories by default apply only to 'post' post type. Custom post types like 'product' need explicit taxonomy registration to use categories.
❓ state_output
advanced2:00remaining
What posts does this complex WP_Query return?
Analyze the query and select the correct description of posts retrieved.
Wordpress
<?php $args = [ 'post_type' => 'post', 'meta_query' => [ 'relation' => 'OR', ['key' => 'color', 'value' => 'red', 'compare' => '='], ['key' => 'size', 'value' => 'large', 'compare' => '='] ], 'category_name' => 'sale' ]; $query = new WP_Query($args); ?>
Attempts:
2 left
💡 Hint
Meta queries combine with 'relation' inside meta_query, category_name filters posts by category.
✗ Incorrect
The query filters posts in 'sale' category and meta conditions combined with OR, so posts matching either meta condition and in 'sale' category are returned.
🧠 Conceptual
expert2:00remaining
Why does WP_Query ignore 'posts_per_page' in some custom queries?
In some custom WP_Query calls, setting 'posts_per_page' has no effect. What is the most likely reason?
Attempts:
2 left
💡 Hint
Check if any parameter disables pagination.
✗ Incorrect
Setting 'nopaging' to true disables pagination and ignores 'posts_per_page'. This is why 'posts_per_page' has no effect.