Raymond
Raymond

Reputation: 137

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. In other words, $query->request looks like this (without compare with empty string):

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. But I need OR, not AND. I tried another variant, with meta_query only.

$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. Even if relation='AND', problem still exists and WP behavior is exactly the same.

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? This behavior is very strange and illogical.

Upvotes: 1

Views: 1303

Answers (2)

Raymond
Raymond

Reputation: 137

Problem in this question is my own mistake. I used filters for modify wp_query logic, and these modifications destroy normal behavior. If you have a similar problem, be sure what you or any plugin \ theme not used filters (f.e. pre_get_posts or any another) which can change results of 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. 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
));

Upvotes: 1

Mickey
Mickey

Reputation: 534

This will not work directly. You need to use WP_Meta_Query here.

Read the following https://codex.wordpress.org/Class_Reference/WP_Meta_Query

Upvotes: 1

Related Questions