admin管理员组文章数量:1290935
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?
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?
Share Improve this question asked Jun 5, 2021 at 11:50 Daan van den BerghDaan van den Bergh 17510 bronze badges1 Answer
Reset to default 0Ah, 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);
}
}
本文标签: wp queryWPQuery() not working as expected
版权声明:本文标题:wp query - WP_Query() not working as expected 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741521279a2383199.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论