0
0
Wordpressframework~20 mins

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

Choose your learning style9 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.