Discover how a tiny URL change can transform your whole website experience!
Why Query parameters in Wordpress? - Purpose & Use Cases
Imagine you want to show different blog posts based on user choices, like category or date, but you have to write separate pages for each option.
Manually creating pages for every filter or search option is slow, confusing, and hard to maintain. It's easy to make mistakes and hard to update.
Query parameters let you pass information in the URL to control what content WordPress shows, so one page can handle many cases automatically.
if ($_GET['category'] == 'news') { showNewsPosts(); } else if ($_GET['category'] == 'events') { showEventPosts(); }
$query = new WP_Query(['category_name' => sanitize_text_field($_GET['category'])]); while ($query->have_posts()) { $query->the_post(); the_title(); }
It makes your site flexible and dynamic, showing exactly what users want without extra pages or code.
A visitor clicks a link like example.com/blog?category=travel and instantly sees all travel posts without you building a special page for travel.
Query parameters pass info through URLs to control content.
They save time by avoiding many separate pages.
They make your site easier to update and more user-friendly.
Practice
WP_Query?Solution
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.Step 2: Identify the correct purpose in WordPress context
They do not change themes, add styles, or create users; they filter content queries.Final Answer:
To filter and retrieve specific posts based on conditions like category or author -> Option DQuick Check:
Query parameters filter posts = D [OK]
- Thinking query parameters change themes
- Confusing query parameters with styling or user management
- Assuming query parameters create new content
WP_Query?Solution
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'.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.Final Answer:
$query = new WP_Query(array('cat' => 5)); -> Option BQuick Check:
Category ID filter uses 'cat' = C [OK]
- Using 'category' instead of 'cat' for category ID
- Passing category ID as 'category_name'
- Using 'category_id' which is invalid
$args = array('author' => 3, 'posts_per_page' => 2);
$query = new WP_Query($args);
while ($query->have_posts()) {
$query->the_post();
echo get_the_title() . ', ';
}Solution
Step 1: Analyze query parameters
'author' => 3 filters posts by author ID 3, 'posts_per_page' => 2 limits results to 2 posts.Step 2: Understand loop output
The loop outputs the titles of these posts separated by commas.Final Answer:
It will display the titles of the latest 2 posts by author with ID 3 -> Option AQuick Check:
Author filter + limit 2 posts = B [OK]
- Assuming 'author' is invalid parameter
- Thinking it shows all posts by author ignoring limit
- Believing it shows posts from any author
$args = array('category' => 'news', 'posts_per_page' => 5);
$query = new WP_Query($args);Solution
Step 1: Check parameter for category slug
To filter by category slug (like 'news'), the correct parameter is 'category_name', not 'category'.Step 2: Validate other parameters
'posts_per_page' accepts a number, so that part is correct.Final Answer:
'category' should be 'category_name' to filter by category slug -> Option AQuick Check:
Category slug filter uses 'category_name' = A [OK]
- Using 'category' instead of 'category_name' for slug
- Thinking 'posts_per_page' must be string
- Confusing 'cat' with slug instead of ID
WP_Query. Which query parameter array will correctly achieve this?Solution
Step 1: Understand how to query multiple categories
To query posts in multiple categories by ID, use 'category__in' with an array of IDs.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.Final Answer:
array('category__in' => array(4, 7)) -> Option CQuick Check:
Multiple category IDs use 'category__in' with array = A [OK]
- Passing multiple IDs as comma string to 'cat'
- Using 'category_name' with numeric IDs
- Passing array to 'cat' which expects single ID
Ready to prove it?
Pass the test and this pattern moves unknown
