📖 Scenario: You are building a WordPress site that shows a list of blog posts. You want to control which posts appear by using query parameters in your code.
🎯 Goal: Create a WordPress query using query parameters to display posts from a specific category and limit the number of posts shown.
📋 What You'll Learn
Create an array called $args with specific query parameters
Add a variable $posts_per_page to control how many posts to show
Use WP_Query with the $args array to get posts
Complete the loop to display the post titles
💡 Why This Matters
🌍 Real World
WordPress developers often need to customize which posts appear on a page by using query parameters to filter posts by category, status, or other criteria.
💼 Career
Understanding WP_Query and query parameters is essential for WordPress theme and plugin developers to create dynamic and customized content displays.
Progress0 / 4 steps
1
Create the query parameters array
Create an array called $args with these exact entries: 'category_name' => 'news' and 'post_status' => 'publish'.
Wordpress
Hint
Use array() to create $args with keys and values exactly as shown.
2
Add a posts per page variable
Add a variable called $posts_per_page and set it to 5. Then add this to the $args array with the key 'posts_per_page'.
Wordpress
Hint
Define $posts_per_page as 5 and add it to $args with the key 'posts_per_page'.
3
Create the WP_Query object
Create a variable called $query and set it to a new WP_Query object using the $args array.
Wordpress
Hint
Use new WP_Query($args) to create the query object.
4
Complete the loop to display post titles
Write a while loop that runs while $query->have_posts() is true. Inside the loop, call $query->the_post() and then output the post title using the_title().
Wordpress
Hint
Use a while loop with $query->have_posts(), call $query->the_post() inside, then output the title with the_title().
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
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 D
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
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 B
Quick Check:
Category ID filter uses 'cat' = C [OK]
Hint: Use 'cat' key for category ID in WP_Query parameters [OK]
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
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 A
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
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 C
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 unknownstrongwhile earning +1 Grip toward your certificate.