Bird
Raised Fist0
Wordpressframework~20 mins

WP_Query class in Wordpress - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the primary purpose of the WP_Query class in WordPress?
easy
A. To style posts with CSS classes
B. To create new posts in the WordPress admin
C. To fetch posts from the database based on custom criteria
D. To manage user roles and permissions

Solution

  1. Step 1: Understand WP_Query's role

    WP_Query is designed to retrieve posts from the database using filters like category, author, or date.
  2. Step 2: Differentiate from other WordPress features

    Creating posts, styling, or managing users are handled by other parts of WordPress, not WP_Query.
  3. Final Answer:

    To fetch posts from the database based on custom criteria -> Option C
  4. Quick Check:

    WP_Query fetches posts = A [OK]
Hint: WP_Query is for getting posts, not creating or styling [OK]
Common Mistakes:
  • Confusing WP_Query with post creation functions
  • Thinking WP_Query styles posts
  • Assuming WP_Query manages users
2. Which of the following is the correct way to start a new WP_Query to get posts from category ID 5?
easy
A. $query = WP_Query(array('cat' => 5));
B. $query = new WP_Query(array('cat' => 5));
C. $query = new WP_Query('category_id' => 5);
D. $query = WP_Query('category' => 5);

Solution

  1. Step 1: Check WP_Query instantiation syntax

    WP_Query must be created with the new keyword and an array of arguments.
  2. Step 2: Verify correct argument key for category

    The correct key for category ID is 'cat', not 'category' or 'category_id'.
  3. Final Answer:

    $query = new WP_Query(array('cat' => 5)); -> Option B
  4. Quick Check:

    Use new WP_Query with array('cat' => 5) = D [OK]
Hint: Use new WP_Query with array and 'cat' key for category ID [OK]
Common Mistakes:
  • Omitting the new keyword
  • Using wrong argument keys like 'category' or 'category_id'
  • Passing arguments without array
3. Given the following code, what will be the output if there are 3 posts in category 10?
$query = new WP_Query(['cat' => 10]);
if ($query->have_posts()) {
  while ($query->have_posts()) {
    $query->the_post();
    echo get_the_title() . ', ';
  }
} else {
  echo 'No posts found';
}
wp_reset_postdata();
medium
A. Titles of the 3 posts separated by commas
B. No posts found
C. Syntax error due to missing semicolons
D. Only the first post title printed

Solution

  1. Step 1: Understand the loop behavior

    The loop runs while have_posts() returns true, printing each post title followed by a comma.
  2. Step 2: Check the number of posts in category 10

    Since there are 3 posts, all 3 titles will be printed separated by commas.
  3. Final Answer:

    Titles of the 3 posts separated by commas -> Option A
  4. Quick Check:

    Loop prints all post titles = C [OK]
Hint: Loop prints all posts if have_posts() is true [OK]
Common Mistakes:
  • Thinking only one post prints
  • Assuming 'No posts found' when posts exist
  • Confusing syntax errors with correct code
4. What is wrong with this WP_Query usage?
$query = new WP_Query(['post_type' => 'product']);
while ($query->have_posts()) {
  the_post();
  echo get_the_title();
}
wp_reset_postdata();
medium
A. Incorrect post_type value
B. Using echo instead of print
C. Missing wp_reset_query() after the loop
D. Missing call to $query->the_post() inside the loop

Solution

  1. Step 1: Check loop method calls

    The loop uses $query->have_posts() but calls the_post() without the query object, which is incorrect.
  2. Step 2: Identify correct method usage

    Inside the loop, it should call $query->the_post() to set up post data properly.
  3. Final Answer:

    Missing call to $query->the_post() inside the loop -> Option D
  4. Quick Check:

    Use $query->the_post() in custom loops = B [OK]
Hint: Use $query->the_post() inside loop, not just the_post() [OK]
Common Mistakes:
  • Calling the_post() without query object in custom loops
  • Confusing wp_reset_postdata() with wp_reset_query()
  • Assuming echo vs print matters here
5. You want to display the latest 5 posts from category 3 but exclude posts tagged with 'featured'. Which WP_Query argument array correctly achieves this?
hard
A. ['cat' => 3, 'posts_per_page' => 5, 'tag__not_in' => [get_term_by('slug', 'featured', 'post_tag')->term_id]]
B. ['category_name' => '3', 'posts_per_page' => 5, 'tag__not_in' => ['featured']]
C. ['cat' => 3, 'posts_per_page' => 5, 'tag__not_in' => ['featured']]
D. ['cat' => 3, 'posts_per_page' => 5, 'tag__not_in' => get_term_by('slug', 'featured', 'post_tag')->term_id]

Solution

  1. Step 1: Identify correct category argument

    Use 'cat' => 3 to filter by category ID 3, not 'category_name' with a number string.
  2. Step 2: Exclude posts by tag ID

    The tag__not_in argument expects an array of tag IDs, so we must get the ID using get_term_by().
  3. Step 3: Check syntax for excluding 'featured' tag

    Using get_term_by('slug', 'featured', 'post_tag')->term_id inside the array correctly fetches the tag ID to exclude.
  4. Final Answer:

    ['cat' => 3, 'posts_per_page' => 5, 'tag__not_in' => [get_term_by('slug', 'featured', 'post_tag')->term_id]] -> Option A
  5. Quick Check:

    Use tag IDs with tag__not_in, not slugs = A [OK]
Hint: Exclude tags by ID array with tag__not_in, not by slug string [OK]
Common Mistakes:
  • Using tag slugs instead of tag IDs in tag__not_in
  • Using category_name with numeric ID
  • Not wrapping tag IDs in an array
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard