📖 Scenario: You are building a WordPress site that shows a list of recent blog posts. To keep the site fast, you want to optimize how you get posts from the database.
🎯 Goal: Learn how to write an optimized WordPress query to fetch only the latest 5 published posts from the 'news' category.
📋 What You'll Learn
Create a query argument array with exact keys and values
Add a variable to limit the number of posts to 5
Use WP_Query with the query arguments
Add the loop to display post titles inside <li> tags
💡 Why This Matters
🌍 Real World
Optimizing queries in WordPress helps your site load faster and handle more visitors smoothly.
💼 Career
WordPress developers often need to write efficient queries to improve site performance and user experience.
Progress0 / 4 steps
1
Create the query arguments array
Create a variable called $args as an array with these exact keys and values: 'category_name' => 'news', 'post_status' => 'publish'.
Wordpress
Hint
Use an associative array with keys 'category_name' and 'post_status'.
2
Add a posts_per_page limit
Add a key 'posts_per_page' with the value 5 to the existing $args array.
Wordpress
Hint
This limits the query to 5 posts only.
3
Create the WP_Query object
Create a variable called $query and assign it a new WP_Query object using the $args array.
Wordpress
Hint
Use the WP_Query class to run the query with your arguments.
4
Add 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 inside an <li> tag using get_the_title(). Wrap the loop inside <ul> tags.
Wordpress
Hint
Use the WordPress loop pattern to show post titles in a list.
Practice
(1/5)
1. What is the main benefit of optimizing queries in WordPress using WP_Query?
easy
A. It reduces server load and speeds up page loading.
B. It automatically fixes broken links on the site.
C. It changes the website's theme dynamically.
D. It increases the number of posts displayed without limits.
Solution
Step 1: Understand query optimization purpose
Query optimization aims to fetch only necessary data to reduce processing time.
Step 2: Connect optimization to performance
By limiting data fetched, server load decreases and pages load faster.
Final Answer:
It reduces server load and speeds up page loading. -> Option A
Quick Check:
Query optimization = faster site [OK]
Hint: Optimized queries fetch less data, making pages load faster [OK]
Common Mistakes:
Thinking optimization changes site design
Believing it fixes unrelated issues like broken links
Assuming optimization increases data fetched
2. Which of the following is the correct way to limit posts to 5 using WP_Query?
easy
A. new WP_Query(array('count' => 5));
B. new WP_Query(array('limit' => 5));
C. new WP_Query(array('max_posts' => 5));
D. new WP_Query(array('posts_per_page' => 5));
Solution
Step 1: Recall WP_Query parameter for limiting posts
The correct parameter to limit posts is 'posts_per_page'.
Step 2: Match parameter with correct syntax
new WP_Query(array('posts_per_page' => 5)); uses 'posts_per_page' correctly with value 5.
Final Answer:
new WP_Query(array('posts_per_page' => 5)); -> Option D
Quick Check:
Limit posts = posts_per_page [OK]
Hint: Use 'posts_per_page' to limit posts in WP_Query [OK]
Common Mistakes:
Using incorrect parameter names like 'limit' or 'count'
Confusing 'posts_per_page' with other parameters
Missing array syntax in WP_Query
3. What will be the effect of this query?
$query = new WP_Query(array('category_name' => 'news', 'posts_per_page' => 3));
medium
A. Fetches all posts from all categories.
B. Fetches 3 posts from any category except 'news'.
C. Fetches 3 posts only from the 'news' category.
D. Fetches posts but ignores the category filter.
Solution
Step 1: Analyze the category filter
The parameter 'category_name' => 'news' limits posts to the 'news' category.
Step 2: Analyze the posts_per_page limit
'posts_per_page' => 3 limits the number of posts fetched to 3.
Final Answer:
Fetches 3 posts only from the 'news' category. -> Option C
A. The 'category' parameter should be 'category_name'.
B. The parameter 'post_per_page' is misspelled; should be 'posts_per_page'.
C. WP_Query cannot accept arrays as arguments.
D. There is no error; the code is correct.
Solution
Step 1: Check parameter names for limiting posts
The correct parameter is 'posts_per_page', not 'post_per_page'.
Step 2: Verify category parameter
The parameter 'category_name' => 'updates' uses the correct syntax for category slug.
Final Answer:
The parameter 'post_per_page' is misspelled; should be 'posts_per_page'. -> Option B
Quick Check:
Correct parameter spelling = posts_per_page [OK]
Hint: Check spelling of WP_Query parameters carefully [OK]
Common Mistakes:
Using 'post_per_page' instead of 'posts_per_page'
Confusing 'category' with 'category_name'
Assuming WP_Query doesn't accept arrays
5. You want to optimize a query to fetch only published posts from the 'events' category, limited to 4 posts, ordered by date descending. Which is the best WP_Query argument array?