[英]Why Wordpress ignores meta_query conditions?
There is a simple query with two meta-conditions. 有一个具有两个元条件的简单查询。
$defaults = array(
'post_type' => 'products',
'orderby' => 'date',
'order' => 'DESC',
);
if($is_deadline) { // true, I checked
$defaults['meta_key'] = 'deadline',
$defaults['meta_type'] = 'NUMERIC',
$defaults['meta_value'] = (int)current_time( 'Ymd' );
$defaults['meta_compare'] = '>=';
$defaults['meta_query'] = array( // this query will be ignored
'relation' => 'OR',
array(
'key' => 'deadline',
'value' => '',
'compare' => '='
)
);
}
But I have problem: Wordpress ignores meta_query block. 但是我有问题:WordPress忽略了meta_query块。 In other words, $query->request looks like this (without compare with empty string):
换句话说,$ query-> request看起来像这样(不与空字符串进行比较):
SELECT adveq_posts.* FROM adveq_posts
INNER JOIN adveq_postmeta ON ( adveq_posts.ID = adveq_postmeta.post_id ) WHERE 1=1
AND (
( adveq_postmeta.meta_key = 'deadline' AND CAST(adveq_postmeta.meta_value AS SIGNED) >= '20160629' )
)
AND adveq_posts.post_type = 'products'
AND (adveq_posts.post_status = 'publish'
OR adveq_posts.post_status = 'private')
GROUP BY adveq_posts.ID
ORDER BY adveq_posts.post_date DESC
This problem fly away if I set relation to 'AND' - meta_query no longer ignored. 如果将关系设置为“ AND”,此问题就会消失-meta_query不再被忽略。 But I need OR, not AND.
但是我需要OR,而不是AND。 I tried another variant, with meta_query only.
我尝试了另一个变体,仅使用meta_query。
$defaults = array(
'post_type' => 'products',
'orderby' => 'date',
'order' => 'DESC',
);
if($is_deadline) { // true, I checked
$defaults['meta_query'] = array(
'relation' => 'OR',
array(
'key' => 'deadline',
'value' => (int)current_time( 'Ymd' ),
'type' => 'NUMERIC',
'compare' => '>='
),
array(
'key' => 'deadline',
'value' => '',
'compare' => '='
)
);
}
But, in this case Wordpress ignores meta_query so much as possible. 但是,在这种情况下,WordPress会尽可能忽略meta_query。 Even if relation='AND', problem still exists and WP behavior is exactly the same.
即使relation ='AND',问题仍然存在,并且WP行为完全相同。
SELECT adveq_posts.* FROM adveq_posts
WHERE 1=1 AND adveq_posts.post_type = 'structured_products'
AND (adveq_posts.post_status = 'publish' OR adveq_posts.post_status = 'private')
ORDER BY adveq_posts.post_date DESC
Any suggestions? 有什么建议么? Perhaps this is a WP core bug?
也许这是WP的核心错误? This behavior is very strange and illogical.
这种行为是非常奇怪和不合逻辑的。
This will not work directly. 这将无法直接工作。 You need to use WP_Meta_Query here.
您需要在此处使用WP_Meta_Query。
Read the following https://codex.wordpress.org/Class_Reference/WP_Meta_Query 阅读以下https://codex.wordpress.org/Class_Reference/WP_Meta_Query
Problem in this question is my own mistake. 这个问题的问题是我自己的错误。 I used filters for modify wp_query logic, and these modifications destroy normal behavior.
我使用过滤器修改wp_query逻辑,这些修改破坏了正常行为。 If you have a similar problem, be sure what you or any plugin \\ theme not used filters (fe pre_get_posts or any another) which can change results of WP_Query.
如果您有类似的问题,请确保您或未使用任何插件\\主题的过滤器(例如pre_get_posts或任何其他过滤器)可以更改WP_Query的结果。
But, if you need combine this logic and gets full benefits of WP_Query, you can pass your own flag in WP_Query constructor, and then check state of it. 但是,如果您需要结合使用此逻辑并获得WP_Query的全部好处,则可以在WP_Query构造函数中传递自己的标志,然后检查其状态。 For example:
例如:
$query = new WP_Query(array(
'post_type' => 'any_post_type',
'orderby' => 'date',
'order' => 'DESC',
'suppress_filters' => false,
'is_your_custom_query' => true, // your custom flag
));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.