0

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?

1 Answer 1

0

Ah, nevermind. Found it.

I'm using another plugin (EDD Hide Download) which hides certain downloads and does that by hooking into the pre_get_posts action, which is triggered in WP_Query. The download in question was marked as hidden, that's why WP_Query didn't return it.

To resolve this, I did the following.

I added an action which was triggered after EDD Hide Downloads:

add_action('plugins_loaded', [$this, 'unhide_downloads'], 11); // EDD Hide Downloads triggers at the default priority 10.

And removed the action hook responsible for inserting the wp_posts.ID NOT IN query:

public function unhide_downloads()
{
    if (function_exists('edd_hide_download')) {
        remove_action('pre_get_posts', [edd_hide_download(), 'pre_get_posts'], 9999);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.