0
0
Wordpressframework~10 mins

Query parameters in Wordpress - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Query parameters
User visits URL with query parameters
WordPress reads URL
Extract query parameters
Modify WP_Query arguments
Run WP_Query with parameters
Display filtered posts or content
WordPress reads the URL query parameters, uses them to adjust the WP_Query arguments, then fetches and shows the filtered content.
Execution Sample
Wordpress
<?php
$args = array('category_name' => $_GET['cat'] ?? '', 'posts_per_page' => 5);
$query = new WP_Query($args);
while ($query->have_posts()) {
  $query->the_post();
  the_title();
}
wp_reset_postdata();
This code reads the 'cat' query parameter from the URL and shows 5 posts from that category.
Execution Table
StepActionQuery Parameter 'cat'WP_Query ArgsPosts Fetched
1User visits URL with ?cat=technologytechnologycategory_name='technology', posts_per_page=5Not fetched yet
2PHP reads $_GET['cat']technologycategory_name='technology', posts_per_page=5Not fetched yet
3Create WP_Query with argstechnologycategory_name='technology', posts_per_page=5Query prepared
4Run have_posts() looptechnologycategory_name='technology', posts_per_page=5Posts from 'technology' category fetched
5Display post titlestechnologycategory_name='technology', posts_per_page=5Titles shown on page
6Reset post datatechnologycategory_name='technology', posts_per_page=5Post data reset
7End of scripttechnologycategory_name='technology', posts_per_page=5Execution complete
💡 All posts matching category 'technology' displayed, loop ends when no more posts.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
$_GET['cat']undefinedtechnologytechnologytechnologytechnology
$argsempty arrayarray('category_name' => 'technology', 'posts_per_page' => 5)samesamesame
$queryundefinedundefinedWP_Query object createdWP_Query object with postsWP_Query object reset
Key Moments - 2 Insights
Why does $_GET['cat'] sometimes cause an error if the URL has no 'cat' parameter?
Because $_GET['cat'] is undefined if the parameter is missing. Using the null coalescing operator (?? '') avoids this by providing a default empty string, as shown in execution_table step 2.
Why do we call wp_reset_postdata() after the loop?
Because WP_Query changes global post data during the loop. Resetting it restores the original state, preventing side effects on other parts of the page, as seen in execution_table step 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of $args after step 3?
Aempty array
Barray('category_name' => 'technology', 'posts_per_page' => 5)
Cundefined
Darray('category_name' => '', 'posts_per_page' => 5)
💡 Hint
Check the 'WP_Query Args' column at step 3 in the execution_table.
At which step does WordPress fetch posts from the database?
AStep 6
BStep 2
CStep 4
DStep 1
💡 Hint
Look at the 'Posts Fetched' column in the execution_table.
If the URL has no 'cat' parameter, what would $args['category_name'] be?
Aempty string ''
Bnull
Cundefined
Dthe string 'cat'
💡 Hint
Refer to the code sample where $_GET['cat'] uses ?? '' to provide a default.
Concept Snapshot
Query parameters in WordPress are read from the URL using $_GET.
Use these parameters to build WP_Query arguments.
Run WP_Query to fetch posts matching those parameters.
Always handle missing parameters safely with ?? operator.
Reset post data after custom queries with wp_reset_postdata().
Full Transcript
When a user visits a WordPress site with query parameters in the URL, WordPress reads these parameters using PHP's $_GET array. For example, if the URL has ?cat=technology, the code reads $_GET['cat'] to get 'technology'. This value is used to build arguments for WP_Query, such as setting 'category_name' to 'technology' and limiting posts to 5. WP_Query then fetches posts matching these arguments. The loop runs to display the post titles. After the loop, wp_reset_postdata() is called to restore the original global post data. This process allows WordPress to dynamically filter and display content based on URL query parameters.