3

I would like to wp_query all my given post_types and apply a filter on one of the four post_types without excluding posts of the other post_types.

For example, I would like to find all posts that have P110612 in it. This code is the WooCommerce sku.

So in order for me to query on the _sku key, I apply this as a meta_query.

But, in order for me to not exclude posts without the meta key _sku, I can make an OR relation, like so:

'meta_query' => [
    'relation' => 'OR',
    [
        'key'     => '_sku',
        'value'   => 'P110612',
        'compare' => 'LIKE',
    ],
    [
        'key'     => '_sku',
        'value'   => 'completely',
        'compare' => 'NOT EXISTS',
    ],
];

What this should do is show all product posts with sku P110612 and all non-product posts where P110612 is somewhere in the content. But, the result is nothing.

Below you'll find the actual query that is being executed by WordPress WP_Query.

I hope you guys can help me out!

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
Tklaversma
  • 51
  • 4

0 Answers0