Bird
0
0

What will the following code output if there are no posts with the tag 'news'?

medium📝 Predict Output Q5 of 15
Wordpress - WordPress Query and Database
What will the following code output if there are no posts with the tag 'news'?
$query = new WP_Query(['tag' => 'news']);
if ($query->have_posts()) {
  echo 'Posts found';
} else {
  echo 'No posts';
}
APosts found
BNo posts
CError
DEmpty string
Step-by-Step Solution
Solution:
  1. Step 1: Understand have_posts() behavior

    have_posts() returns false if no posts match the query.
  2. Step 2: Analyze condition with no matching posts

    Since no posts have tag 'news', condition is false, so 'No posts' is echoed.
  3. Final Answer:

    No posts -> Option B
  4. Quick Check:

    have_posts() false means no posts found [OK]
Quick Trick: have_posts() false means no posts matched [OK]
Common Mistakes:
  • Assuming have_posts() returns true even if no posts
  • Expecting an error when no posts found

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Wordpress Quizzes