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 | Show 10 more comments1 Answer
Reset to default -1The 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
版权声明:本文标题:WP_Query for multiple post types just shows one 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736637788a1945921.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
var_dump( $posts->request );
after thenew WP_Query
call. Do that on both your snippets and share what the dump output. – Sally CJ Commented Feb 23, 2024 at 16:33pre_get_posts
filters active? It's very odd to see-1
wrapped in quotes, and you've also specifiedcategory
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:35category
to thepost_type
- and even if I reduce the query to filter only bypost_type
it does not reply with my podcasts, as soon as I addpost
to the array. – wepli23 Commented Feb 23, 2024 at 19:06var_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