What is the purpose of the <code>WP_Query</code> class in WordPress?
The <code>WP_Query</code> class is used to fetch posts from the WordPress database based on specific criteria like post type, category, date, and more. It helps display custom lists of posts.
Click to reveal answer
beginner
How do you create a new WP_Query object to get the latest 5 posts?
You create it like this: $query = new WP_Query(['posts_per_page' => 5]); This fetches the 5 most recent posts.
Click to reveal answer
beginner
What method do you use to check if a WP_Query has posts to show?
Use the have_posts() method. It returns true if there are posts matching the query, false otherwise.
Click to reveal answer
intermediate
Explain the loop structure when using WP_Query to display posts.
You use a while loop: while ($query->have_posts()) { $query->the_post(); /* display post */ } This loops through each post and sets up post data for template tags.
Click to reveal answer
intermediate
Why should you call wp_reset_postdata() after a custom WP_Query loop?
It restores the global $post variable to the main query’s current post. This prevents conflicts with other parts of the page that rely on the main query.
Click to reveal answer
Which argument limits the number of posts returned by WP_Query?
Aposts_per_page
Bpost_limit
Cmax_posts
Dlimit_posts
✗ Incorrect
The correct argument is posts_per_page to limit how many posts are fetched.
What does the_post() do inside a WP_Query loop?
AResets the query
BSets up global post data for template tags
CEnds the loop
DFetches the next post from the database
✗ Incorrect
the_post() prepares global variables so template tags like the_title() work correctly.
Which method checks if there are posts available in a WP_Query object?
Aget_posts()
Bpost_exists()
Cquery_posts()
Dhave_posts()
✗ Incorrect
have_posts() returns true if the query has posts to loop through.
What is the purpose of wp_reset_postdata() after a custom query loop?
AStarts a new query
BDeletes the custom query
CRestores the main query’s post data
DClears all posts from memory
✗ Incorrect
It resets global post data to the main query to avoid conflicts.
How do you query posts from a specific category using WP_Query?
AUse 'category_name' argument
BUse 'cat_id' argument
CUse 'category' argument
DUse 'category_slug' argument
✗ Incorrect
The correct argument is category_name with the category slug.
Describe how to use the WP_Query class to fetch and display posts with a custom loop.
Think about the steps to get posts and show them safely on a page.
You got /5 concepts.
Explain why resetting post data with wp_reset_postdata() is important after a custom query.
Consider what happens if you don’t reset after a custom loop.
You got /3 concepts.
Practice
(1/5)
1. What is the primary purpose of the WP_Query class in WordPress?
easy
A. To style posts with CSS classes
B. To create new posts in the WordPress admin
C. To fetch posts from the database based on custom criteria
D. To manage user roles and permissions
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 C
Quick Check:
WP_Query fetches posts = A [OK]
Hint: WP_Query is for getting posts, not creating or styling [OK]
Common Mistakes:
Confusing WP_Query with post creation functions
Thinking WP_Query styles posts
Assuming WP_Query manages users
2. Which of the following is the correct way to start a new WP_Query to get posts from category ID 5?
easy
A. $query = WP_Query(array('cat' => 5));
B. $query = new WP_Query(array('cat' => 5));
C. $query = new WP_Query('category_id' => 5);
D. $query = WP_Query('category' => 5);
Solution
Step 1: Check WP_Query instantiation syntax
WP_Query must be created with the new keyword 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 B
Quick Check:
Use new WP_Query with array('cat' => 5) = D [OK]
Hint: Use new WP_Query with array and 'cat' key for category ID [OK]
Common Mistakes:
Omitting the new keyword
Using wrong argument keys like 'category' or 'category_id'
Passing arguments without array
3. Given the following code, what will be the output if there are 3 posts in category 10?
$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();
medium
A. Titles of the 3 posts separated by commas
B. No posts found
C. Syntax error due to missing semicolons
D. Only the first post title printed
Solution
Step 1: Understand the loop behavior
The loop runs while have_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 A
Quick Check:
Loop prints all post titles = C [OK]
Hint: Loop prints all posts if have_posts() is true [OK]
Common Mistakes:
Thinking only one post prints
Assuming 'No posts found' when posts exist
Confusing syntax errors with correct code
4. What is wrong with this WP_Query usage?
$query = new WP_Query(['post_type' => 'product']);
while ($query->have_posts()) {
the_post();
echo get_the_title();
}
wp_reset_postdata();
medium
A. Incorrect post_type value
B. Using echo instead of print
C. Missing wp_reset_query() after the loop
D. Missing call to $query->the_post() inside the loop
Solution
Step 1: Check loop method calls
The loop uses $query->have_posts() but calls the_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 D
Quick Check:
Use $query->the_post() in custom loops = B [OK]
Hint: Use $query->the_post() inside loop, not just the_post() [OK]
Common Mistakes:
Calling the_post() without query object in custom loops
Confusing wp_reset_postdata() with wp_reset_query()
Assuming echo vs print matters here
5. You want to display the latest 5 posts from category 3 but exclude posts tagged with 'featured'. Which WP_Query argument array correctly achieves this?