Bird
0
0

Why might this loop cause unexpected results?

medium📝 Debug Q7 of 15
Wordpress - Custom Theme Development
Why might this loop cause unexpected results?
$query = new WP_Query(['posts_per_page' => 3]); if ($query->have_posts()) { while ($query->have_posts()) { $query->the_post(); } } echo get_the_title();
Aposts_per_page should be a string, not an integer
BWP_Query does not support posts_per_page argument
Cthe_post() must be called before have_posts()
Dget_the_title() is called outside the loop without resetting post data
Step-by-Step Solution
Solution:
  1. Step 1: Analyze function calls after the loop

    After the loop ends, get_the_title() is called but the global post data is still set to the last post of the custom query.
  2. Step 2: Explain why this is unexpected

    Without calling wp_reset_postdata(), the global post remains changed, which can cause unexpected output.
  3. Final Answer:

    get_the_title() is called outside the loop without resetting post data -> Option D
  4. Quick Check:

    Reset post data after custom loops [OK]
Quick Trick: Always reset post data after custom WP_Query loops [OK]
Common Mistakes:
  • Not resetting post data after custom loops
  • Misunderstanding posts_per_page type

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Wordpress Quizzes