Bird
Raised Fist0
Wordpressframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of The Loop in WordPress?
easy
A. To manage user login sessions
B. To create new posts in the database
C. To style the website layout
D. To display posts one by one on a page

Solution

  1. Step 1: Understand The Loop's role

    The Loop is a WordPress structure that cycles through posts to display them.
  2. Step 2: Identify its main function

    It outputs posts one by one on the page, showing content to visitors.
  3. Final Answer:

    To display posts one by one on a page -> Option D
  4. Quick Check:

    The Loop shows posts = D [OK]
Hint: The Loop shows posts on pages one at a time [OK]
Common Mistakes:
  • Thinking The Loop creates posts
  • Confusing The Loop with styling or login
  • Assuming The Loop manages database directly
2. Which of the following is the correct way to start The Loop in a WordPress theme file?
easy
A. if ( have_posts() ) : while ( have_posts() ) : the_post();
B. while ( the_post() ) : have_posts();
C. if ( the_post() ) : while ( have_posts() );
D. for ( have_posts(); the_post(); ) :

Solution

  1. Step 1: Recall The Loop syntax

    The Loop starts by checking if posts exist with have_posts(), then loops with while ( have_posts() ).
  2. Step 2: Confirm correct function order

    Inside the loop, the_post() sets up post data for each iteration.
  3. Final Answer:

    if ( have_posts() ) : while ( have_posts() ) : the_post(); -> Option A
  4. Quick Check:

    Correct loop syntax = A [OK]
Hint: Check if posts exist before looping with have_posts() [OK]
Common Mistakes:
  • Using the_post() before have_posts()
  • Mixing function order in the loop
  • Using for loop syntax instead of while
3. Given this code snippet, what posts will be displayed?
$custom_query = new WP_Query(['category_name' => 'news', 'posts_per_page' => 2]);
if ($custom_query->have_posts()) :
  while ($custom_query->have_posts()) : $custom_query->the_post();
    the_title();
  endwhile;
endif;
wp_reset_postdata();
medium
A. Only posts from the default query ignoring 'news'
B. All posts from the 'news' category
C. Only the first 2 posts from the 'news' category
D. No posts will be displayed due to syntax error

Solution

  1. Step 1: Analyze WP_Query parameters

    The query requests posts from category 'news' and limits to 2 posts.
  2. Step 2: Understand the loop behavior

    The loop runs through posts found by $custom_query, showing only those 2 posts.
  3. Final Answer:

    Only the first 2 posts from the 'news' category -> Option C
  4. Quick Check:

    WP_Query limits posts = A [OK]
Hint: posts_per_page limits how many posts show [OK]
Common Mistakes:
  • Assuming all posts show ignoring posts_per_page
  • Confusing default query with custom query
  • Missing wp_reset_postdata() after custom loop
4. What is wrong with this code snippet?
if ( have_posts() ) {
  while ( have_posts() ) {
    the_post;
    the_title();
  }
}
medium
A. Missing parentheses after the_post function call
B. Incorrect use of curly braces instead of colons
C. the_title() should be called before the_post
D. have_posts() should be called only once outside the loop

Solution

  1. Step 1: Check function calls inside the loop

    The function the_post is missing parentheses, so it is not called properly.
  2. Step 2: Verify syntax correctness

    Functions must have parentheses even if no arguments: the_post(); is correct.
  3. Final Answer:

    Missing parentheses after the_post function call -> Option A
  4. Quick Check:

    Function calls need () = B [OK]
Hint: Always add () when calling functions in PHP [OK]
Common Mistakes:
  • Forgetting parentheses on function calls
  • Confusing syntax styles (curly braces vs colons)
  • Calling the_title() before the_post()
5. You want to display a list of posts excluding category 'news' and showing only posts with the custom field 'featured' set to 'yes'. Which WP_Query arguments correctly achieve this?
hard
A. ['category__not_in' => get_cat_ID('news'), 'meta_key' => 'featured', 'meta_value' => 'yes']
B. ['cat' => '-'.get_cat_ID('news'), 'meta_query' => [['key' => 'featured', 'value' => 'yes']]]
C. ['category_name' => 'news', 'meta_key' => 'featured', 'meta_value' => 'yes']
D. ['category__in' => [get_cat_ID('news')], 'meta_key' => 'featured', 'meta_value' => 'yes']

Solution

  1. Step 1: Exclude category 'news'

    Using 'cat' => '-ID' excludes a category by its ID. get_cat_ID('news') gets the ID.
  2. Step 2: Filter posts with custom field 'featured' = 'yes'

    Use meta_query with key and value to filter posts by custom fields.
  3. Step 3: Combine both conditions correctly

    ['cat' => '-'.get_cat_ID('news'), 'meta_query' => [['key' => 'featured', 'value' => 'yes']]] uses negative cat ID and meta_query array properly.
  4. Final Answer:

    ['cat' => '-'.get_cat_ID('news'), 'meta_query' => [['key' => 'featured', 'value' => 'yes']]] -> Option B
  5. Quick Check:

    Exclude category with negative ID + meta_query = C [OK]
Hint: Use negative cat ID to exclude categories in WP_Query [OK]
Common Mistakes:
  • Using category__not_in without meta_query for custom fields
  • Including category instead of excluding
  • Using meta_key/meta_value without meta_query array
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard