Recall & Review
beginner
What is the purpose of the <code>WP_Query</code> class in WordPress?The <code>WP_Query</code> class is used to fetch posts from the WordPress database based on specific criteria like post type, category, date, and more. It helps display custom lists of posts.Click to reveal answer
beginner
How do you create a new
WP_Query object to get the latest 5 posts?You create it like this: <br>
$query = new WP_Query(['posts_per_page' => 5]);<br>This fetches the 5 most recent posts.Click to reveal answer
beginner
What method do you use to check if a
WP_Query has posts to show?Use the
have_posts() method. It returns true if there are posts matching the query, false otherwise.Click to reveal answer
intermediate
Explain the loop structure when using
WP_Query to display posts.You use a while loop:<br>
while ($query->have_posts()) { $query->the_post(); /* display post */ }<br>This loops through each post and sets up post data for template tags.Click to reveal answer
intermediate
Why should you call
wp_reset_postdata() after a custom WP_Query loop?It restores the global
$post variable to the main query’s current post. This prevents conflicts with other parts of the page that rely on the main query.Click to reveal answer
Which argument limits the number of posts returned by
WP_Query?✗ Incorrect
The correct argument is
posts_per_page to limit how many posts are fetched.What does
the_post() do inside a WP_Query loop?✗ Incorrect
the_post() prepares global variables so template tags like the_title() work correctly.Which method checks if there are posts available in a
WP_Query object?✗ Incorrect
have_posts() returns true if the query has posts to loop through.What is the purpose of
wp_reset_postdata() after a custom query loop?✗ Incorrect
It resets global post data to the main query to avoid conflicts.
How do you query posts from a specific category using
WP_Query?✗ Incorrect
The correct argument is
category_name with the category slug.Describe how to use the
WP_Query class to fetch and display posts with a custom loop.Think about the steps to get posts and show them safely on a page.
You got /5 concepts.
Explain why resetting post data with
wp_reset_postdata() is important after a custom query.Consider what happens if you don’t reset after a custom loop.
You got /3 concepts.