admin管理员组

文章数量:1125467

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?

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?

Share Improve this question edited Feb 26, 2024 at 19:53 wepli23 asked Feb 23, 2024 at 15:48 wepli23wepli23 113 bronze badges 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 Commented Feb 23, 2024 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 Commented Feb 23, 2024 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 Commented Feb 23, 2024 at 19:06
  • @SallyCJ Thank you! I've done that and added the dumps above. – wepli23 Commented Feb 23, 2024 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 Commented Feb 24, 2024 at 13:00
 |  Show 10 more comments

1 Answer 1

Reset to default -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.

本文标签: WPQuery for multiple post types just shows one