0
0
Wordpressframework~20 mins

WP_Query class in Wordpress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
WP_Query Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this WP_Query output?
Consider this WordPress code snippet using WP_Query. What posts will it retrieve and display?
Wordpress
<?php
$query = new WP_Query(['category_name' => 'news', 'posts_per_page' => 3]);
if ($query->have_posts()) {
  while ($query->have_posts()) {
    $query->the_post();
    echo get_the_title() . '\n';
  }
}
wp_reset_postdata();
?>
AIt outputs the titles of the latest 3 posts in the 'news' category.
BIt outputs all posts except those in the 'news' category.
CIt outputs the titles of the first 3 posts ever published, ignoring category.
DIt outputs the titles of 3 random posts from any category.
Attempts:
2 left
💡 Hint
Look at the 'category_name' and 'posts_per_page' parameters in WP_Query.
📝 Syntax
intermediate
1:30remaining
Which WP_Query argument array is valid?
Which of the following WP_Query argument arrays will NOT cause a syntax error?
A['post_type' => 'post', 'posts_per_page' => 'five']
B['post_type' => 'post' 'posts_per_page' => 5]
C['post_type' => 'post', posts_per_page => 5]
D['post_type' => 'page', 'posts_per_page' => 5]
Attempts:
2 left
💡 Hint
Check for missing commas and correct string keys.
state_output
advanced
2:00remaining
What is the value of $count after this WP_Query?
Given this code, what is the value of $count after execution?
Wordpress
<?php
$query = new WP_Query(['tag' => 'featured']);
$count = 0;
if ($query->have_posts()) {
  while ($query->have_posts()) {
    $query->the_post();
    $count++;
  }
}
wp_reset_postdata();
?>
AThe number of posts tagged with 'featured'.
BAlways 0 because $count is reset inside the loop.
CThe total number of posts in the database.
DAlways 1 because the loop runs once.
Attempts:
2 left
💡 Hint
Count increments inside the loop for each post found.
🔧 Debug
advanced
2:30remaining
Why does this WP_Query return no posts?
This code returns no posts even though posts exist. What is the problem?
Wordpress
<?php
$query = new WP_Query(['category' => 'news']);
if ($query->have_posts()) {
  while ($query->have_posts()) {
    $query->the_post();
    echo get_the_title() . '\n';
  }
}
wp_reset_postdata();
?>
AThe posts_per_page parameter is missing, so no posts are returned by default.
B'category' is not a valid WP_Query parameter; it should be 'category_name' or 'cat'.
CThe loop is missing a call to rewind_posts(), so it skips all posts.
DThe wp_reset_postdata() call is inside the loop, causing it to reset early.
Attempts:
2 left
💡 Hint
Check the parameter names for filtering by category.
🧠 Conceptual
expert
3:00remaining
Which WP_Query argument combination fetches posts from multiple categories?
You want to fetch posts that belong to either category ID 4 or 7. Which WP_Query argument array achieves this?
A['cat' => '4,7']
B['category__and' => [4, 7]]
C['category__in' => [4, 7]]
D['category_name' => '4,7']
Attempts:
2 left
💡 Hint
Check the difference between category__in and category__and parameters.