我想wp_query我所有给定的post_types并在四个post_types中的一个上应用过滤器而不排除其他post_types的帖子。
例如,我想查找其中包含P110612
的所有帖子。这段代码是WooCommerce sku。
因此,为了让我查询_sku
密钥,我将其应用为meta_query。
但是,为了让我不排除没有元键_sku
的帖子,我可以制作OR relation
,如下所示:
'meta_query' => [
'relation' => 'OR',
[
'key' => '_sku',
'value' => 'P110612',
'compare' => 'LIKE',
],
[
'key' => '_sku',
'value' => 'completely',
'compare' => 'NOT EXISTS',
],
];
应该做什么显示所有产品帖子,其中包含sku P110612以及所有非产品帖子,其中P110612位于内容的某个位置。但是,结果一无所获。
您将在下面找到WordPress WP_Query正在执行的实际查询。
我希望你们能帮助我!
SELECT prefix_posts.*
FROM prefix_posts
LEFT JOIN prefix_postmeta ON ( prefix_posts.ID = prefix_postmeta.post_id )
LEFT JOIN prefix_postmeta AS mt1 ON (prefix_posts.ID = mt1.post_id AND mt1.meta_key = '_sku' )
WHERE 1=1
AND prefix_posts.ID NOT IN (2,10,11,12)
AND (
(
(prefix_posts.post_title LIKE '%P110612%')
OR (prefix_posts.post_excerpt LIKE '%P110612%')
OR (prefix_posts.post_content LIKE '%P110612%')
)
)
AND (prefix_posts.post_password = '')
AND (
( prefix_postmeta.meta_key = '_sku' AND prefix_postmeta.meta_value = 'P110612' )
OR mt1.post_id IS NULL
)
AND prefix_posts.post_type IN ('page', 'newsitem', 'partner', 'product')
AND ((prefix_posts.post_status = 'publish'))
GROUP BY prefix_posts.ID
ORDER BY prefix_posts.post_title LIKE '%P110612%' DESC, prefix_posts.post_date DESC