0
0
Wordpressframework~20 mins

The Loop and query in Wordpress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Loop Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this WordPress Loop output?

Consider this WordPress Loop code snippet inside a template file:

if ( have_posts() ) {
  while ( have_posts() ) {
    the_post();
    the_title();
  }
} else {
  echo 'No posts found';
}

What will this code output if there are 3 published posts?

Wordpress
if ( have_posts() ) {
  while ( have_posts() ) {
    the_post();
    the_title();
  }
} else {
  echo 'No posts found';
}
AThe string 'No posts found' printed
BThe titles of all 3 posts printed one after another
CAn error because the_post() is missing inside the loop
DOnly the title of the first post printed
Attempts:
2 left
💡 Hint

Remember that have_posts() checks if there are posts left, and the_post() moves the pointer to the next post.

📝 Syntax
intermediate
2:00remaining
Which WP_Query argument array fetches only posts in category ID 5?

You want to create a custom query that fetches posts only from category with ID 5. Which argument array is correct?

A{ 'category_name' => 5 }
B{ 'category_id' => 5 }
C{ 'category' => 5 }
D{ 'cat' => 5 }
Attempts:
2 left
💡 Hint

Check the official WP_Query parameters for category filtering.

🔧 Debug
advanced
2:00remaining
Why does this custom query loop show no posts even though posts exist?

Look at this code snippet:

$args = [
  'post_type' => 'post',
  'posts_per_page' => 5
];
$query = new WP_Query($args);

if ( have_posts() ) {
  while ( have_posts() ) {
    the_post();
    the_title();
  }
} else {
  echo 'No posts found';
}

Why does it show 'No posts found' even if posts exist?

Wordpress
$args = [
  'post_type' => 'post',
  'posts_per_page' => 5
];
$query = new WP_Query($args);

if ( have_posts() ) {
  while ( have_posts() ) {
    the_post();
    the_title();
  }
} else {
  echo 'No posts found';
}
ABecause the code uses the main loop functions instead of the custom query's methods
BBecause 'posts_per_page' should be set to -1 to fetch all posts
CBecause 'post_type' => 'post' is invalid and should be 'posts'
DBecause WP_Query requires a call to wp_reset_query() before the loop
Attempts:
2 left
💡 Hint

Think about which loop functions belong to the main query and which belong to a custom query.

state_output
advanced
2:00remaining
What is the value of $post_count after this loop?

Given this code snippet inside a template:

$post_count = 0;
if ( have_posts() ) {
  while ( have_posts() ) {
    the_post();
    $post_count++;
  }
}
echo $post_count;

If there are 7 posts in the main query, what will be printed?

Wordpress
$post_count = 0;
if ( have_posts() ) {
  while ( have_posts() ) {
    the_post();
    $post_count++;
  }
}
echo $post_count;
A7
B0
C1
DAn error because $post_count is not global
Attempts:
2 left
💡 Hint

Count how many times the loop runs if there are 7 posts.

🧠 Conceptual
expert
3:00remaining
Which statement about The Loop and WP_Query is TRUE?

Choose the correct statement about how The Loop and WP_Query interact in WordPress.

AYou can use the_post() without calling have_posts() first in any loop safely.
BCalling wp_reset_postdata() resets the main query to the first post and clears all custom queries.
CThe main query uses global functions like have_posts() and the_post(), while custom WP_Query objects require calling methods on the object.
DWP_Query automatically merges all custom queries into the main query after execution.
Attempts:
2 left
💡 Hint

Think about how WordPress separates the main query from custom queries.