The WP_Query class helps you get posts or content from your WordPress site easily. It lets you ask for specific posts based on rules you set.
WP_Query class in Wordpress
$query = new WP_Query( array( 'key' => 'value', 'key2' => 'value2' ) ); if ( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post(); // Your code to display the post } wp_reset_postdata(); }
The array inside WP_Query sets the rules for which posts to get.
Always use wp_reset_postdata() after your loop to restore original post data.
$query = new WP_Query( array( 'posts_per_page' => 5 ) );
$query = new WP_Query( array( 'category_name' => 'news' ) );
$query = new WP_Query( array( 'author' => 3 ) );
$query = new WP_Query( array( 'meta_key' => 'color', 'meta_value' => 'blue' ) );
This code gets the 3 newest posts from the 'news' category and prints their titles line by line.
<?php // Create a new query to get 3 latest posts from category 'news' $query = new WP_Query( array( 'posts_per_page' => 3, 'category_name' => 'news' ) ); if ( $query->have_posts() ) { echo "Latest news posts:\n"; while ( $query->have_posts() ) { $query->the_post(); echo get_the_title() . "\n"; } wp_reset_postdata(); } else { echo "No posts found."; } ?>
WP_Query does not change the main query unless you replace it; it creates a separate query.
Use have_posts() and the_post() to loop through results safely.
Remember to reset post data with wp_reset_postdata() after your custom loop.
WP_Query helps you fetch posts with custom rules.
Use it to show posts by category, author, date, or custom fields.
Always loop with have_posts() and reset after with wp_reset_postdata().
Practice
WP_Query class in WordPress?Solution
Step 1: Understand WP_Query's role
WP_Query is designed to retrieve posts from the database using filters like category, author, or date.Step 2: Differentiate from other WordPress features
Creating posts, styling, or managing users are handled by other parts of WordPress, not WP_Query.Final Answer:
To fetch posts from the database based on custom criteria -> Option CQuick Check:
WP_Query fetches posts = A [OK]
- Confusing WP_Query with post creation functions
- Thinking WP_Query styles posts
- Assuming WP_Query manages users
WP_Query to get posts from category ID 5?Solution
Step 1: Check WP_Query instantiation syntax
WP_Query must be created with thenewkeyword and an array of arguments.Step 2: Verify correct argument key for category
The correct key for category ID is'cat', not 'category' or 'category_id'.Final Answer:
$query = new WP_Query(array('cat' => 5)); -> Option BQuick Check:
Use new WP_Query with array('cat' => 5) = D [OK]
- Omitting the new keyword
- Using wrong argument keys like 'category' or 'category_id'
- Passing arguments without array
$query = new WP_Query(['cat' => 10]);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
echo get_the_title() . ', ';
}
} else {
echo 'No posts found';
}
wp_reset_postdata();Solution
Step 1: Understand the loop behavior
The loop runs whilehave_posts()returns true, printing each post title followed by a comma.Step 2: Check the number of posts in category 10
Since there are 3 posts, all 3 titles will be printed separated by commas.Final Answer:
Titles of the 3 posts separated by commas -> Option AQuick Check:
Loop prints all post titles = C [OK]
- Thinking only one post prints
- Assuming 'No posts found' when posts exist
- Confusing syntax errors with correct code
$query = new WP_Query(['post_type' => 'product']);
while ($query->have_posts()) {
the_post();
echo get_the_title();
}
wp_reset_postdata();Solution
Step 1: Check loop method calls
The loop uses$query->have_posts()but callsthe_post()without the query object, which is incorrect.Step 2: Identify correct method usage
Inside the loop, it should call$query->the_post()to set up post data properly.Final Answer:
Missing call to $query->the_post() inside the loop -> Option DQuick Check:
Use $query->the_post() in custom loops = B [OK]
- Calling the_post() without query object in custom loops
- Confusing wp_reset_postdata() with wp_reset_query()
- Assuming echo vs print matters here
Solution
Step 1: Identify correct category argument
Use'cat' => 3to filter by category ID 3, not 'category_name' with a number string.Step 2: Exclude posts by tag ID
Thetag__not_inargument expects an array of tag IDs, so we must get the ID usingget_term_by().Step 3: Check syntax for excluding 'featured' tag
Usingget_term_by('slug', 'featured', 'post_tag')->term_idinside the array correctly fetches the tag ID to exclude.Final Answer:
['cat' => 3, 'posts_per_page' => 5, 'tag__not_in' => [get_term_by('slug', 'featured', 'post_tag')->term_id]] -> Option AQuick Check:
Use tag IDs with tag__not_in, not slugs = A [OK]
- Using tag slugs instead of tag IDs in tag__not_in
- Using category_name with numeric ID
- Not wrapping tag IDs in an array
Ready to prove it?
Pass the test and this pattern moves unknown
