Custom queries help you get exactly the posts or data you want from WordPress. They let you pick specific items instead of all content.
Why custom queries retrieve specific data in Wordpress
<?php
$args = array(
'key' => 'value',
// other query parameters
);
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
// display post data
}
wp_reset_postdata();
}
?>The $args array sets rules for which posts to get.
Always use wp_reset_postdata() after custom queries to restore original data.
<?php $args = array('category_name' => 'news'); $query = new WP_Query($args);
<?php $args = array('author' => 5); $query = new WP_Query($args);
<?php $args = array('posts_per_page' => 3); $query = new WP_Query($args);
<?php $args = array('meta_key' => 'color', 'meta_value' => 'blue'); $query = new WP_Query($args);
This code gets the latest 2 posts from the 'news' category and prints their titles. It shows how custom queries retrieve specific data by setting rules.
<?php /* Template to show posts from category 'news' */ $args = array('category_name' => 'news', 'posts_per_page' => 2); $query = new WP_Query($args); echo "Posts in 'news' category:\n"; if ($query->have_posts()) { while ($query->have_posts()) { $query->the_post(); echo get_the_title() . "\n"; } } else { echo "No posts found.\n"; } wp_reset_postdata(); ?>
Custom queries run slower if you ask for many posts or complex filters.
Always reset post data after custom queries to avoid conflicts with main queries.
Use custom queries when you want to show filtered or limited posts, not for all content.
Custom queries let you pick exactly which posts to get from WordPress.
You set rules in an array to filter posts by category, author, date, or custom fields.
Always reset post data after running a custom query to keep your site working well.
Practice
Solution
Step 1: Understand the purpose of custom queries
Custom queries allow you to select posts based on specific rules like category, author, or date.Step 2: Compare with other options
Changing themes, updating core, or creating users are unrelated to querying posts.Final Answer:
To retrieve specific posts based on filters like category or author -> Option AQuick Check:
Custom queries = filter posts [OK]
- Confusing custom queries with theme changes
- Thinking custom queries update WordPress core
- Assuming custom queries create users
Solution
Step 1: Check WP_Query syntax
WP_Query expects an array of arguments inside new WP_Query([...]).Step 2: Identify correct array syntax
$query = new WP_Query(['category_name' => 'news']); uses the correct PHP array syntax with key 'category_name' and value 'news'.Final Answer:
$query = new WP_Query(['category_name' => 'news']); -> Option BQuick Check:
Correct WP_Query syntax = $query = new WP_Query(['category_name' => 'news']); [OK]
- Passing query as string instead of array
- Missing 'new' keyword before WP_Query
- Using wrong argument keys like 'category'
$args = ['author' => 5, 'posts_per_page' => 3];
$query = new WP_Query($args);
while ($query->have_posts()) {
$query->the_post();
echo get_the_title() . '
';
}Solution
Step 1: Analyze query arguments
The 'author' => 5 limits posts to author ID 5, 'posts_per_page' => 3 limits to 3 posts.Step 2: Understand loop output
The loop prints titles of these 3 posts by author 5.Final Answer:
The latest 3 posts by author with ID 5 -> Option AQuick Check:
Author 5 + 3 posts = The latest 3 posts by author with ID 5 [OK]
- Ignoring posts_per_page limit
- Confusing author filter as exclusion
- Assuming random posts without filter
$args = ['category_name' => 'news'];
$query = new WP_Query($args);
while ($query->have_posts()) {
$query->the_post();
echo get_the_title() . '
';
}Solution
Step 1: Check query and loop correctness
The query and loop syntax are correct and will run.Step 2: Identify missing reset
After a custom query loop, you must call wp_reset_postdata() to restore global post data.Final Answer:
It does not reset post data after the loop -> Option DQuick Check:
Missing wp_reset_postdata() = It does not reset post data after the loop [OK]
- Forgetting wp_reset_postdata() after loop
- Thinking category_name is invalid
- Assuming have_posts() is missing
Solution
Step 1: Check category and date filters
'category_name' => 'events' and 'date_query' with 'after' => '2023-01-01' correctly filter posts after that date.Step 2: Verify custom field filter
'meta_query' with key 'featured' and value 'yes' filters posts with that custom field set.Step 3: Compare options
{'category_name' => 'events', 'date_query' => [['after' => '2023-01-01']], 'meta_query' => [['key' => 'featured', 'value' => 'yes']]} uses correct keys and nested arrays for date_query and meta_query. Others have wrong keys or values.Final Answer:
{'category_name' => 'events', 'date_query' => [['after' => '2023-01-01']], 'meta_query' => [['key' => 'featured', 'value' => 'yes']]} -> Option CQuick Check:
Correct filters + meta_query = {'category_name' => 'events', 'date_query' => [['after' => '2023-01-01']], 'meta_query' => [['key' => 'featured', 'value' => 'yes']]} [OK]
- Using 'category' instead of 'category_name'
- Wrong date_query keys like 'before' instead of 'after'
- Confusing meta_key/meta_value with meta_query array
Ready to prove it?
Pass the test and this pattern moves unknown
