Bird
0
0

What is wrong with this WP_Query usage?

medium📝 Debug Q14 of 15
Wordpress - WordPress Query and Database
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();
AIncorrect post_type value
BUsing echo instead of print
CMissing wp_reset_query() after the loop
DMissing call to <code>$query->the_post()</code> inside the loop
Step-by-Step Solution
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]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Wordpress Quizzes