I'm building a plugin for EDD which adds a custom metabox with checkbox option meta_key _xy_global_product
.
So far, so good.
In the frontend, I'm trying to filter posts which have this meta_key set to 1.
This is my code:
$args = [
'meta_query' => [
[
'key' => '_xy_global_product',
'value' => 1
]
],
'post_type' => 'download', // Custom post type, created by Easy Digital Downloads.
'posts_per_page' => -1
];
$global_products = new WP_Query($args);
$global_products
returns an empty array, because the query built by WP_Query()
is the following:
SELECT wp_posts.* FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1 AND wp_posts.ID NOT IN (4816,4473,4163) AND (
( wp_postmeta.meta_key = '_xy_global_product' AND wp_postmeta.meta_value = '1' )
) AND wp_posts.post_type = 'download' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'cancelled' OR wp_posts.post_status = 'edd_subscription' OR wp_posts.post_status = 'refunded' OR wp_posts.post_status = 'failed' OR wp_posts.post_status = 'revoked' OR wp_posts.post_status = 'abandoned' OR wp_posts.post_status = 'processing' OR wp_posts.post_status = 'active' OR wp_posts.post_status = 'inactive' OR wp_posts.post_status = 'preapproval_pending' OR wp_posts.post_status = 'preapproval' OR wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC
The expected result (in my test case) would be a post with ID 4163. However, on production it could be a list of posts.
The following part is preventing the query from being successful: wp_posts.ID NOT IN (4816,4473,4163)
because when I remove that, it returns the expected result.
How do I get rid of this part in the query?