0

I am using following meta_query to search blog post according to following search criteria,

search logic :

  1. if expire date set, check the expiry of the post, otherwise just include it
  2. if country($_country) in the filters, search for that particular country with above expire date rule. otherwise just apply the expire date rule.

meta_query:

$expire_meta_1 = array(
    'key' => '_simple_fields_fieldGroupID_3_fieldID_7_numInSet_0',
    'value' => '',
    'compare' => 'NOT EXISTS'
);

$expire_meta_2 = array(
    'key' => '_simple_fields_fieldGroupID_3_fieldID_7_numInSet_0',
    'value' => date("Y-m-d"),
    'compare' => '>=',
    'type' => 'DATE'
);

if($_country!=''){
    $country_meta = array(
        'key' => '_simple_fields_fieldGroupID_3_fieldID_8_numInSet_0',
        'value' => $_country,
        'compare' => '='
    );
    $args['meta_query'] = array( 'relation' => 'AND', array( 'relation' => 'OR', $expire_meta_1, $expire_meta_2), $country_meta);
} else{
    $args['meta_query'] = array( 'relation' => 'OR', $expire_meta_1, $expire_meta_2);
}

the issue is, if it is country search, post should set the expire date. but I need to select the posts also not set the expire date.(if set check the expiry, that is anyway working).

But if I am not searching for specific country, the expire date rule(rule 1) is working properly.

Anybody have any idea what is my mistake. Any help would be appreciate. I hope question is clear, otherwise I can provide more details, please let me know. Thanks in advance.

8
  • I do not understand what is the point of this condition date is greater than or equal to todayDate OR date is not empty it is just same as date is greater than or equal to todayDate isn't it ?
    – Sumit
    Mar 13, 2016 at 7:01
  • @Sumit the point is, in some post we are setting the expire date, but rest we doesn't set it. So whenever it sets we need to check the expiry rule, otherwise we can include that post regardless of expiry date as there is no expiry date set to post. Mar 13, 2016 at 7:10
  • Okay so you mean if country is set then pull the posts (country AND expire) OR (No expire at all regardless of country)
    – Sumit
    Mar 13, 2016 at 7:50
  • sorry for the confusion, expire date set from post itself, then the country is from the filters in search. First the expire rule is, if expiry date set in the post, then check the expiry, otherwise post without expire dates should be in the search results. then the country filter, if there is country search for country plus expire rule which I describe avove Mar 13, 2016 at 7:58
  • Okay thanks but in your question you are missing the relation between all three ? I guess you want ((country AND expiry) OR no expiry) but you've (country AND (expiry OR no expiry)) ? So if country is not there, then there is no mean of expire at all because you've placed AND as a relation.
    – Sumit
    Mar 13, 2016 at 8:06

1 Answer 1

1

Finally I found the real issue after few hours of debugging.

I'm using simple fields to save the expire date and country values. So in simple field even we are not setting anything it is still save empty string in the database. So when I check the non existing fields, it is not giving correct results, however when I search of empty string(like below) it is giving me the correct output.

$expire_meta_1 = array(
    'key' => '_simple_fields_fieldGroupID_3_fieldID_7_numInSet_0',
    'value' => '',
    'compare' => '='
);

I should thanks to @Sumit to help me to figure out the real issue.

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.