Bird
0
0

Given the following code, what will be the output if there are 3 posts in category 10?

medium📝 component behavior Q13 of 15
Wordpress - WordPress Query and Database
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();
ATitles of the 3 posts separated by commas
BNo posts found
CSyntax error due to missing semicolons
DOnly the first post title printed
Step-by-Step Solution
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]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Wordpress Quizzes