Bird
Raised Fist0
Wordpressframework~10 mins

Query parameters in Wordpress - Step-by-Step Execution

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

Practice

(1/5)
1. What is the main purpose of query parameters in WordPress when used with WP_Query?
easy
A. To create new user accounts automatically
B. To change the WordPress theme dynamically
C. To add custom CSS styles to posts
D. To filter and retrieve specific posts based on conditions like category or author

Solution

  1. Step 1: Understand the role of query parameters

    Query parameters are used to specify conditions for retrieving posts, such as filtering by category or author.
  2. Step 2: Identify the correct purpose in WordPress context

    They do not change themes, add styles, or create users; they filter content queries.
  3. Final Answer:

    To filter and retrieve specific posts based on conditions like category or author -> Option D
  4. Quick Check:

    Query parameters filter posts = D [OK]
Hint: Query parameters filter posts by conditions like category or author [OK]
Common Mistakes:
  • Thinking query parameters change themes
  • Confusing query parameters with styling or user management
  • Assuming query parameters create new content
2. Which of the following is the correct way to query posts from category with ID 5 using WP_Query?
easy
A. $query = new WP_Query(array('category' => 5));
B. $query = new WP_Query(array('cat' => 5));
C. $query = new WP_Query(array('category_name' => 5));
D. $query = new WP_Query(array('category_id' => 5));

Solution

  1. Step 1: Recall correct parameter for category ID

    The correct parameter to filter by category ID is 'cat', not 'category', 'category_name', or 'category_id'.
  2. Step 2: Verify syntax correctness

    $query = new WP_Query(array('cat' => 5)); uses 'cat' with the ID 5 correctly in an array for WP_Query.
  3. Final Answer:

    $query = new WP_Query(array('cat' => 5)); -> Option B
  4. Quick Check:

    Category ID filter uses 'cat' = C [OK]
Hint: Use 'cat' key for category ID in WP_Query parameters [OK]
Common Mistakes:
  • Using 'category' instead of 'cat' for category ID
  • Passing category ID as 'category_name'
  • Using 'category_id' which is invalid
3. What will be the result of this code snippet?
$args = array('author' => 3, 'posts_per_page' => 2);
$query = new WP_Query($args);
while ($query->have_posts()) {
$query->the_post();
echo get_the_title() . ', ';
}
medium
A. It will display the titles of the latest 2 posts by author with ID 3
B. It will display all posts by author with ID 3
C. It will cause a syntax error because 'author' is invalid
D. It will display 2 posts from any author

Solution

  1. Step 1: Analyze query parameters

    'author' => 3 filters posts by author ID 3, 'posts_per_page' => 2 limits results to 2 posts.
  2. Step 2: Understand loop output

    The loop outputs the titles of these posts separated by commas.
  3. Final Answer:

    It will display the titles of the latest 2 posts by author with ID 3 -> Option A
  4. Quick Check:

    Author filter + limit 2 posts = B [OK]
Hint: Author ID filters posts; posts_per_page limits number shown [OK]
Common Mistakes:
  • Assuming 'author' is invalid parameter
  • Thinking it shows all posts by author ignoring limit
  • Believing it shows posts from any author
4. Identify the error in this query parameter usage:
$args = array('category' => 'news', 'posts_per_page' => 5);
$query = new WP_Query($args);
medium
A. 'category' should be 'category_name' to filter by category slug
B. 'posts_per_page' must be a string, not a number
C. 'category' should be 'cat' with category slug
D. No error, this code works correctly

Solution

  1. Step 1: Check parameter for category slug

    To filter by category slug (like 'news'), the correct parameter is 'category_name', not 'category'.
  2. Step 2: Validate other parameters

    'posts_per_page' accepts a number, so that part is correct.
  3. Final Answer:

    'category' should be 'category_name' to filter by category slug -> Option A
  4. Quick Check:

    Category slug filter uses 'category_name' = A [OK]
Hint: Use 'category_name' for slug, 'cat' for ID in queries [OK]
Common Mistakes:
  • Using 'category' instead of 'category_name' for slug
  • Thinking 'posts_per_page' must be string
  • Confusing 'cat' with slug instead of ID
5. You want to display posts that belong to category ID 4 or category ID 7 using WP_Query. Which query parameter array will correctly achieve this?
hard
A. array('category_name' => '4,7')
B. array('cat' => '4,7')
C. array('category__in' => array(4, 7))
D. array('cat' => array(4, 7))

Solution

  1. Step 1: Understand how to query multiple categories

    To query posts in multiple categories by ID, use 'category__in' with an array of IDs.
  2. Step 2: Evaluate each option

    array('cat' => '4,7') uses a string with comma which is invalid for 'cat'. array('category_name' => '4,7') uses 'category_name' with numbers which expects slugs. array('cat' => array(4, 7)) uses 'cat' with an array which is invalid.
  3. Final Answer:

    array('category__in' => array(4, 7)) -> Option C
  4. Quick Check:

    Multiple category IDs use 'category__in' with array = A [OK]
Hint: Use 'category__in' with array for multiple category IDs [OK]
Common Mistakes:
  • Passing multiple IDs as comma string to 'cat'
  • Using 'category_name' with numeric IDs
  • Passing array to 'cat' which expects single ID
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