生成的查询要求所有条件为真:post_type 为 social 或 post AND category_name 为 social AND post_status 为 publish。
一个简单的解决方案是改用WP_Query:
$query = new WP_Query(array(
'post_type' => array('social', 'post'),
'category_name' => 'social',
'post_status' => 'publish'
));
$posts = $query->posts;
然后,您可以使用posts_where 过滤器来修改查询的where 部分:
add_filter( 'posts_where', 'my_posts_where', 10, 2 );
function my_posts_where( $where, $query ) {
...modify $where...
return $where;
}
我将跳过字符串操作部分,因为那里没有具体内容。无论如何,$where 看起来像这样(用我的 WordPress 安装测试):
"AND ( wp_term_relationships.term_taxonomy_id IN (<number>))
AND wp_posts.post_type IN ('social', 'post') AND
((wp_posts.post_status = 'publish'))"
你需要把它转换成这样的:
"AND ( ((wp_term_relationships.term_taxonomy_id IN (<number>)
AND wp_posts.post_type = 'post') OR (wp_posts.post_type = 'social'))
AND (wp_posts.post_status = 'publish'))"
最后,您需要确保my_posts_where 不会破坏其他查询。我可能只是在$query 对象中添加一些指示符,以在过滤器中识别此特定查询。