📖 Scenario: You are building a WordPress site for a local library. The library wants to display a list of books separately from regular blog posts. Each book is stored as a custom post type called book.Your task is to create a simple query to fetch and display these books on a page.
🎯 Goal: Build a WordPress template snippet that queries the book custom post type and displays the title of each book.
📋 What You'll Learn
Create a query to get posts of the custom post type book
Set the number of posts to fetch to 5
Loop through the query results and display each book's title
Reset the post data after the loop
💡 Why This Matters
🌍 Real World
Libraries, bookstores, or any site that needs to show custom content types separately from blog posts can use this technique.
💼 Career
Understanding custom post type queries is essential for WordPress developers building custom themes and plugins.
Progress0 / 4 steps
1
Create the initial query arguments
Create an array called $args with these exact entries: 'post_type' => 'book' and 'posts_per_page' => 5.
Wordpress
Hint
Use an associative array with keys 'post_type' and 'posts_per_page'.
2
Create the WP_Query object
Create a variable called $book_query and assign it a new WP_Query object using the $args array.
Wordpress
Hint
Use new WP_Query($args) to create the query object.
3
Loop through the query results and display titles
Write a while loop that runs while $book_query->have_posts() is true. Inside the loop, call $book_query->the_post() and then output the book title using the_title().
Wordpress
Hint
Use while ($book_query->have_posts()) and inside call $book_query->the_post() before the_title().
4
Reset post data after the loop
After the loop, call wp_reset_postdata() to restore the original post data.
Wordpress
Hint
Call wp_reset_postdata() after the loop to avoid conflicts with other queries.
Practice
(1/5)
1. What is the main purpose of using post_type in a WP_Query in WordPress?
easy
A. To fetch posts of a specific custom post type
B. To change the theme of the website
C. To update the WordPress core version
D. To delete all posts from the database
Solution
Step 1: Understand WP_Query parameters
The post_type parameter in WP_Query specifies which type of posts to fetch, such as posts, pages, or custom types.
Step 2: Identify the purpose of post_type
It filters the query to only return posts of the given type, allowing you to get custom content.
Final Answer:
To fetch posts of a specific custom post type -> Option A
Quick Check:
post_type filters posts by type = A [OK]
Hint: Remember: post_type selects which content type to get [OK]
Common Mistakes:
Confusing post_type with theme or plugin settings
Thinking post_type deletes or updates posts
Using post_type to change site appearance
2. Which of the following is the correct way to query a custom post type named 'product' using WP_Query?
easy
A. new WP_Query(array('post_type' => 'product'));
B. new WP_Query('post_type' = 'product');
C. WP_Query('post_type' => 'product');
D. new WP_Query(array('posttype' => 'product'));
Solution
Step 1: Check WP_Query syntax
The correct syntax uses new WP_Query(array('post_type' => 'product')) with an associative array and the correct key post_type.
Step 2: Identify errors in other options
new WP_Query('post_type' = 'product'); uses wrong assignment syntax, C misses new keyword, and D misspells post_type.
Final Answer:
new WP_Query(array('post_type' => 'product')); -> Option A
Quick Check:
Correct array syntax with 'post_type' key = B [OK]
Hint: Use new WP_Query with array and 'post_type' key [OK]
Common Mistakes:
Using '=' instead of '=>' in array
Omitting 'new' keyword
Misspelling 'post_type' as 'posttype'
3. Given the following code, what will be the output if there are 3 'event' posts?
$query = new WP_Query(array('post_type' => 'event')); if ($query->have_posts()) { while ($query->have_posts()) { $query->the_post(); echo get_the_title() . ', '; } } wp_reset_postdata();
medium
A. Syntax error due to missing semicolons
B. No output because 'event' is not a default post type
C. Only the first event post title printed
D. Titles of all 3 event posts separated by commas
Solution
Step 1: Understand WP_Query loop behavior
The loop runs while have_posts() returns true, iterating over all posts of type 'event'.
Step 2: Output titles of all posts
Inside the loop, get_the_title() prints each post's title followed by a comma, so all 3 titles appear separated by commas.
Final Answer:
Titles of all 3 event posts separated by commas -> Option D
Quick Check:
Loop prints all titles = C [OK]
Hint: Loop with have_posts() prints all matching posts [OK]
Common Mistakes:
Assuming only default post types work
Thinking loop prints only one post
Confusing syntax errors with correct code
4. Identify the error in this custom post type query code:
$args = array('post_type' => 'book'); $query = WP_Query($args); if ($query->have_posts()) { while ($query->have_posts()) { $query->the_post(); the_title(); } } wp_reset_postdata();
medium
A. Using the_title() inside the loop is invalid
B. Incorrect post_type name 'book'
C. Missing 'new' keyword before WP_Query instantiation
D. Missing wp_reset_postdata() call
Solution
Step 1: Check WP_Query instantiation
WP_Query must be instantiated with the new keyword. The code misses new before WP_Query($args).
Step 2: Verify other parts
The post_type 'book' is valid if registered, wp_reset_postdata() is present, and the_title() is valid inside the loop.
Final Answer:
Missing 'new' keyword before WP_Query instantiation -> Option C
Quick Check:
WP_Query needs 'new' keyword = D [OK]
Hint: Always use 'new' when creating WP_Query objects [OK]
Common Mistakes:
Forgetting 'new' keyword
Assuming post_type names are always wrong
Thinking wp_reset_postdata() is optional
5. You want to query a custom post type 'movie' but only show posts published in 2023. Which WP_Query argument array correctly achieves this?