Custom post type queries let you get specific groups of content in WordPress. This helps you show only the posts you want, like products or events.
Custom post type queries in Wordpress
$query = new WP_Query([ 'post_type' => 'your_post_type', 'posts_per_page' => 5, 'order' => 'ASC', 'orderby' => 'date', // other query parameters ]);
Replace 'your_post_type' with the slug of your custom post type.
WP_Query returns posts matching your criteria, which you can loop through.
$query = new WP_Query([ 'post_type' => 'book', 'posts_per_page' => 5 ]);
$query = new WP_Query([ 'post_type' => 'event', 'orderby' => 'date', 'order' => 'ASC' ]);
$query = new WP_Query([ 'post_type' => ['book', 'movie'], 'posts_per_page' => 10 ]);
This code gets the 3 newest posts from the 'product' custom post type and prints their titles line by line. If no products exist, it shows a message.
<?php // Query 3 latest 'product' posts $query = new WP_Query([ 'post_type' => 'product', 'posts_per_page' => 3 ]); if ($query->have_posts()) { while ($query->have_posts()) { $query->the_post(); echo get_the_title() . "\n"; } } else { echo "No products found."; } wp_reset_postdata(); ?>
Always call wp_reset_postdata() after a custom query to restore global post data.
You can combine custom post type queries with other filters like taxonomy or meta queries.
Use pre_get_posts hook to modify main queries for custom post types if needed.
Custom post type queries help you get specific content types in WordPress.
Use WP_Query with the post_type parameter to fetch posts.
Loop through results and reset post data after to keep WordPress working smoothly.
Practice
post_type in a WP_Query in WordPress?Solution
Step 1: Understand WP_Query parameters
Thepost_typeparameter inWP_Queryspecifies which type of posts to fetch, such as posts, pages, or custom types.Step 2: Identify the purpose of
It filters the query to only return posts of the given type, allowing you to get custom content.post_typeFinal Answer:
To fetch posts of a specific custom post type -> Option AQuick Check:
post_type filters posts by type = A [OK]
- Confusing post_type with theme or plugin settings
- Thinking post_type deletes or updates posts
- Using post_type to change site appearance
WP_Query?Solution
Step 1: Check WP_Query syntax
The correct syntax usesnew WP_Query(array('post_type' => 'product'))with an associative array and the correct keypost_type.Step 2: Identify errors in other options
new WP_Query('post_type' = 'product'); uses wrong assignment syntax, C missesnewkeyword, and D misspellspost_type.Final Answer:
new WP_Query(array('post_type' => 'product')); -> Option AQuick Check:
Correct array syntax with 'post_type' key = B [OK]
- Using '=' instead of '=>' in array
- Omitting 'new' keyword
- Misspelling 'post_type' as 'posttype'
$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();Solution
Step 1: Understand WP_Query loop behavior
The loop runs whilehave_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 DQuick Check:
Loop prints all titles = C [OK]
- Assuming only default post types work
- Thinking loop prints only one post
- Confusing syntax errors with correct 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();Solution
Step 1: Check WP_Query instantiation
WP_Query must be instantiated with thenewkeyword. The code missesnewbeforeWP_Query($args).Step 2: Verify other parts
The post_type 'book' is valid if registered,wp_reset_postdata()is present, andthe_title()is valid inside the loop.Final Answer:
Missing 'new' keyword before WP_Query instantiation -> Option CQuick Check:
WP_Query needs 'new' keyword = D [OK]
- Forgetting 'new' keyword
- Assuming post_type names are always wrong
- Thinking wp_reset_postdata() is optional
WP_Query argument array correctly achieves this?Solution
Step 1: Understand filtering by date in WP_Query
To filter posts by year, use thedate_queryparameter with an array specifying the year.Step 2: Evaluate each option
{ 'post_type' => 'movie', 'date_query' => array(array('year' => 2023)) } correctly usesdate_querywith year 2023. { 'post_type' => 'movie', 'year' => 2023 } uses an invalid 'year' key. { 'post_type' => 'movie', 'meta_key' => 'year', 'meta_value' => 2023 } filters by meta fields, not post date. { 'post_type' => 'movie', 'post_status' => '2023' } misuses post_status.Final Answer:
{ 'post_type' => 'movie', 'date_query' => array(array('year' => 2023)) } -> Option BQuick Check:
Use date_query for year filtering = A [OK]
- Using 'year' directly instead of date_query
- Confusing meta_key with post date
- Misusing post_status for date filtering
Ready to prove it?
Pass the test and this pattern moves unknown
