我正在为EDD构建一个插件,它添加了一个带有复选框选项meta\\u key的自定义元框_xy_global_product
.
到目前为止,一切顺利。
在前端,我尝试过滤将此meta\\u键设置为1的帖子。
这是我的代码:
$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
返回空数组,因为查询是由
WP_Query()
如下所示:
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
预期结果(在我的测试用例中)将是ID为4163的帖子。然而,在制作时,它可能是一个职位列表。
以下部分阻止查询成功:wp_posts.ID NOT IN (4816,4473,4163)
因为当我删除它时,它会返回预期的结果。
如何在查询中删除此部分?
SO网友:Daan van den Bergh
啊,没关系。找到了。
我正在使用另一个插件(EDD Hide Download),它隐藏某些下载,并通过挂接到pre_get_posts
操作,在中触发WP_Query
. 有问题的下载被标记为隐藏,这就是为什么WP\\u查询没有返回它。
为了解决这个问题,我做了以下工作。
我添加了一个在EDD隐藏下载后触发的操作:
add_action(\'plugins_loaded\', [$this, \'unhide_downloads\'], 11); // EDD Hide Downloads triggers at the default priority 10.
并移除了负责插入
wp_posts.ID NOT IN
查询:
public function unhide_downloads()
{
if (function_exists(\'edd_hide_download\')) {
remove_action(\'pre_get_posts\', [edd_hide_download(), \'pre_get_posts\'], 9999);
}
}