0
0
Wordpressframework~10 mins

WP_Query class in Wordpress - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - WP_Query class
Create WP_Query object with args
WP_Query runs SQL query
Fetch posts matching criteria
Store posts in object property
Use loop: have_posts() and the_post()
Output post data
Reset post data after loop
This flow shows how WP_Query creates a query, fetches posts, and lets you loop through them.
Execution Sample
Wordpress
<?php
$query = new WP_Query(['category_name' => 'news', 'posts_per_page' => 2]);
if ($query->have_posts()) {
  while ($query->have_posts()) {
    $query->the_post();
    the_title();
  }
}
wp_reset_postdata();
This code queries 2 posts from the 'news' category and outputs their titles.
Execution Table
StepActionEvaluationResult
1Create WP_Query with argscategory_name='news', posts_per_page=2WP_Query object with query prepared
2Call have_posts()Check if posts existTrue (posts found)
3Call the_post()Set current post to first postCurrent post set to post 0
4Call the_title()Output post 0 titleTitle of post 0 displayed
5Call have_posts()Check if more postsTrue (post 1 exists)
6Call the_post()Set current post to second postCurrent post set to post 1
7Call the_title()Output post 1 titleTitle of post 1 displayed
8Call have_posts()Check if more postsFalse (no more posts)
9Exit loopNo more postsLoop ends
10Call wp_reset_postdata()Reset global post dataGlobal post restored
💡 Loop ends when have_posts() returns false, meaning no more posts to show.
Variable Tracker
VariableStartAfter Step 3After Step 6Final
$query->current_post-1011
$query->posts[][post0, post1][post0, post1][post0, post1]
$post (global)original postpost0post1original post
Key Moments - 3 Insights
Why does have_posts() return true before the loop starts?
Because WP_Query fetches posts on creation, have_posts() checks if there are posts left to show. See execution_table rows 2 and 5.
What does the_post() do inside the loop?
the_post() moves the internal pointer to the next post and sets global $post. See execution_table rows 3 and 6.
Why call wp_reset_postdata() after the loop?
It restores the original global $post so other parts of the page work correctly. See execution_table row 10.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of $query->current_post after step 6?
A1
B0
C-1
D2
💡 Hint
Check variable_tracker row for $query->current_post after step 6.
At which step does have_posts() return false, ending the loop?
AStep 7
BStep 9
CStep 8
DStep 10
💡 Hint
See execution_table row where have_posts() returns false.
If posts_per_page was set to 1, how would the execution table change?
ALoop would run twice as usual
BLoop would run only once, steps 5-7 would be skipped
Chave_posts() would always return false
Dthe_post() would not set current post
💡 Hint
Look at how posts_per_page limits posts and affects loop iterations.
Concept Snapshot
WP_Query class lets you fetch posts with custom criteria.
Create with args array, then use have_posts() and the_post() to loop.
Inside loop, use template tags like the_title() to show data.
Always call wp_reset_postdata() after to restore global post.
It works like a custom post fetcher for WordPress themes and plugins.
Full Transcript
WP_Query is a WordPress class to get posts based on filters like category or number. You create a WP_Query object with arguments. It runs a database query and stores matching posts. Then you use have_posts() to check if posts exist and the_post() to move through them one by one. Inside the loop, you can output post info like titles. After the loop, call wp_reset_postdata() to reset global post data. This process helps you show custom lists of posts on your site.