0

I have an issue, that I do not really understand. I hope, you can help me. I would like to have a WP_query for multiple post types:

$query = array(
  'category_name' => $category,
  'post_type' => array('podcast', 'post')
);
$posts = new WP_Query($query);

It does just reply my posts, no podcasts.

Dump from $posts->request is

SELECT   wp_posts.*
            FROM wp_posts
            LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
            LEFT JOIN wp_term_relationships AS tt1 ON (wp_posts.ID = tt1.object_id)
            WHERE 1=1  AND ( 
  wp_term_relationships.term_taxonomy_id IN (152) 
  AND 
  tt1.term_taxonomy_id IN (414,432)
) AND ((wp_posts.post_type = 'podcast' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'acf-disabled' OR wp_posts.post_status = 'tribe-ea-success' OR wp_posts.post_status = 'tribe-ea-failed' OR wp_posts.post_status = 'tribe-ea-schedule' OR wp_posts.post_status = 'tribe-ea-pending' OR wp_posts.post_status = 'tribe-ea-draft' 
OR wp_posts.post_status = 'private')) OR (wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'acf-disabled' OR wp_posts.post_status = 'tribe-ea-success' OR wp_posts.post_status = 'tribe-ea-failed' OR wp_posts.post_status = 'tribe-ea-schedule' OR wp_posts.post_status = 'tribe-ea-pending' OR wp_posts.post_status = 'tribe-ea-draft' 
OR wp_posts.post_status = 'private')))
            GROUP BY wp_posts.ID
            ORDER BY wp_posts.post_date DESC

But...

$query = array(
  'category_name' => $category,
  'post_type' => array('podcast')
);
$posts = new WP_Query($query);

...does really display the podcasts.

Dump from $posts->request is

SELECT   wp_posts.*
            FROM wp_posts
            LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
            WHERE 1=1  AND ( 
  wp_term_relationships.term_taxonomy_id IN (414,432)
) AND ((wp_posts.post_type = 'podcast' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'acf-disabled' OR wp_posts.post_status = 'tribe-ea-success' OR wp_posts.post_status = 'tribe-ea-failed' OR wp_posts.post_status = 'tribe-ea-schedule' OR wp_posts.post_status = 'tribe-ea-pending' OR wp_posts.post_status = 'tribe-ea-draft' 
OR wp_posts.post_status = 'private')))
            GROUP BY wp_posts.ID
            ORDER BY wp_posts.post_date DESC

So why doe all podcasts disappear in that query, when I do add 'post' to the array? I thought the array would display posts AND podcasts?

15
  • Try inspecting the SQL query, e.g. add var_dump( $posts->request ); after the new WP_Query call. Do that on both your snippets and share what the dump output.
    – Sally CJ
    Feb 23 at 16:33
  • do you have pre_get_posts filters active? It's very odd to see -1 wrapped in quotes, and you've also specified category but does the podcasts post type support it? Posts have to match all the things you specified, so it won't match posts in that category then ignore it for podcasts
    – Tom J Nowell
    Feb 23 at 16:35
  • @TomJNowell Thank you! Yes, I added category to the post_type - and even if I reduce the query to filter only by post_type it does not reply with my podcasts, as soon as I add post to the array.
    – wepli23
    Feb 23 at 19:06
  • @SallyCJ Thank you! I've done that and added the dumps above.
    – wepli23
    Feb 23 at 19:19
  • 1
    Well, that's weird, but in that case, with your 1st snippet, what's the output of var_dump( $posts ); ? Have you tried deactivating plugins, and how did it go? And can you edit your post/question and add your post type registration code?
    – Sally CJ
    Feb 24 at 13:00

1 Answer 1

-1

The issue you're experiencing is due to how WordPress handles queries with multiple post types. When you specify multiple post types in the post_type parameter of a WP_Query, WordPress will only return posts that match all of the specified post types. In your case, when you include both 'post' and 'podcast' in the post_type array, WordPress tries to find posts that are of both types, which is likely not what you intend.

If you want to query posts from multiple post types separately, you'll need to create multiple queries or use a custom SQL query to achieve the desired result. Here's an example of how you could modify your code to query posts from both 'post' and 'podcast' types separately:

$query_posts = array(
  'category_name' => $category,
  'post_type' => 'post',                      
  'posts_per_page'  => '-1',
  'orderby' => 'date'
);
$posts = new WP_Query($query_posts);

$query_podcasts = array(
  'category_name' => $category,
  'post_type' => 'podcast',                      
  'posts_per_page'  => '-1',
  'orderby' => 'date'
);
$podcasts = new WP_Query($query_podcasts);

This way, you'll have two separate queries, one for posts and one for podcasts, and you can loop through the results of each query separately to display the content.

2
  • Thank you, Ali! Okay, then I have to try two querys. But then I have to manage to display the results all together - so merge them and sort them all by date. How can I do that? That would be much easier with just one query... But what I still do not understand: Why does the query show all my posts in my first example array('podcast', 'post')- although the posts do not match the podcast type?
    – wepli23
    Feb 24 at 6:37
  • 1
    @AliAlizadegan "WordPress tries to find posts that are of both types" - a post can only ever have one single post type, and when the post_type is an array of post types, WordPress will search for posts where the type is in that array, just like you could see from the $posts->request dump in the revised question. I.e. Basically, there's a (wp_posts.post_type = 'podcast' ...) OR (wp_posts.post_type = 'post' ...) ( note the OR ) in the SQL statement.
    – Sally CJ
    Feb 24 at 8:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.