Bird
Raised Fist0
Wordpressframework~20 mins

Custom post type queries in Wordpress - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Custom Post Type Query Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this WP_Query for a custom post type?

Consider this WordPress code querying a custom post type 'book'. What does echo $query->found_posts; output?

Wordpress
<?php
$args = [
  'post_type' => 'book',
  'posts_per_page' => 5,
  'post_status' => 'publish'
];
$query = new WP_Query($args);
echo $query->found_posts;
?>
AThe number of posts returned on this page, which can be less than 5
BThe total number of published 'book' posts in the database
CAlways 5, because posts_per_page is 5
DThe total number of posts of any post type in the database
Attempts:
2 left
💡 Hint

Think about what found_posts means in WP_Query.

❓ component_behavior
intermediate
2:00remaining
How does 'meta_query' affect a custom post type query?

Given this WP_Query for 'movie' post type with a meta_query filtering by 'rating' > 7, what posts are returned?

Wordpress
<?php
$args = [
  'post_type' => 'movie',
  'meta_query' => [
    [
      'key' => 'rating',
      'value' => 7,
      'compare' => '>',
      'type' => 'NUMERIC'
    ]
  ]
];
$query = new WP_Query($args);
?>
AOnly posts with 'rating' meta key, ignoring post type
BAll 'movie' posts regardless of 'rating' meta value
CPosts of any type with 'rating' meta value greater than 7
DOnly 'movie' posts with a 'rating' meta value greater than 7
Attempts:
2 left
💡 Hint

Remember that 'post_type' limits the query to that type, and 'meta_query' filters by meta fields.

📝 Syntax
advanced
2:00remaining
Which WP_Query argument array correctly queries 'event' posts ordered by custom field 'event_date'?

Choose the correct argument array to query 'event' custom post type ordered by 'event_date' meta key ascending.

A{ 'post_type' => 'event', 'orderby' => 'event_date', 'order' => 'ASC' }
B{ 'post_type' => 'event', 'meta_key' => 'event_date', 'orderby' => 'meta_value', 'order' => 'DESC' }
C{ 'post_type' => 'event', 'meta_key' => 'event_date', 'orderby' => 'meta_value_num', 'order' => 'ASC' }
D{ 'post_type' => 'event', 'meta_key' => 'event_date', 'orderby' => 'date', 'order' => 'ASC' }
Attempts:
2 left
💡 Hint

Ordering by a numeric custom field requires specific keys.

🔧 Debug
advanced
2:00remaining
Why does this WP_Query for 'product' post type return no posts?

Examine this code snippet. Why does it return zero posts?

Wordpress
<?php
$args = [
  'post_type' => 'product',
  'tax_query' => [
    [
      'taxonomy' => 'product_cat',
      'field' => 'slug',
      'terms' => 'electronics'
    ]
  ],
  'posts_per_page' => 10
];
$query = new WP_Query($args);
?>
AThe 'terms' value 'electronics' does not exist in 'product_cat' taxonomy
BThe taxonomy 'product_cat' is not registered for 'product' post type
CMissing 'relation' key in 'tax_query' array causes failure
DThe 'field' should be 'id' not 'slug' for taxonomy queries
Attempts:
2 left
💡 Hint

Check if the term slug exists in the taxonomy.

🧠 Conceptual
expert
2:00remaining
What is the effect of 'suppress_filters' => false in a custom post type query?

In a WP_Query for a custom post type, what does setting 'suppress_filters' => false do?

AAllows filters like 'posts_where' and 'posts_join' to modify the SQL query
BPrevents any filters from running on the query, improving performance
CAutomatically caches the query results for faster repeated queries
DDisables pagination and returns all posts matching the query
Attempts:
2 left
💡 Hint

Think about how WordPress filters interact with 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

  1. 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.
  2. 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.
  3. Final Answer:

    To fetch posts of a specific custom post type -> Option A
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    new WP_Query(array('post_type' => 'product')); -> Option A
  4. 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

  1. Step 1: Understand WP_Query loop behavior

    The loop runs while have_posts() returns true, iterating over all posts of type 'event'.
  2. 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.
  3. Final Answer:

    Titles of all 3 event posts separated by commas -> Option D
  4. 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

  1. Step 1: Check WP_Query instantiation

    WP_Query must be instantiated with the new keyword. The code misses new before WP_Query($args).
  2. 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.
  3. Final Answer:

    Missing 'new' keyword before WP_Query instantiation -> Option C
  4. 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?
hard
A. { 'post_type' => 'movie', 'year' => 2023 }
B. { 'post_type' => 'movie', 'date_query' => array(array('year' => 2023)) }
C. { 'post_type' => 'movie', 'meta_key' => 'year', 'meta_value' => 2023 }
D. { 'post_type' => 'movie', 'post_status' => '2023' }

Solution

  1. Step 1: Understand filtering by date in WP_Query

    To filter posts by year, use the date_query parameter with an array specifying the year.
  2. Step 2: Evaluate each option

    { 'post_type' => 'movie', 'date_query' => array(array('year' => 2023)) } correctly uses date_query with 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.
  3. Final Answer:

    { 'post_type' => 'movie', 'date_query' => array(array('year' => 2023)) } -> Option B
  4. Quick Check:

    Use date_query for year filtering = A [OK]
Hint: Use date_query array to filter posts by year [OK]
Common Mistakes:
  • Using 'year' directly instead of date_query
  • Confusing meta_key with post date
  • Misusing post_status for date filtering
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard