I am using the Advanced Custom Fields plugin, and I want to do a query to show all posts where a "date" field is before today and where another custom field (a report) has a value.

For now I have this, but I am always getting all past events.

       'post_type' => 'activiteit',
   'meta_key' => 'activiteit_datum',
   'post_status' => 'publish',
   'orderby' => 'meta_value',
   'order' => 'DESC',
   'meta_query' => array(
      'relation' => 'AND',
      array( 'key' => 'activiteit_verslag','compare'=>'EXISTS'),
      array( 'key' => 'activiteit_datum', 'compare' => '<', 'value' => $fullDate,'type' => 'DATE')


  • 1
    There is a spurious single quote before "EXISTS". Is that in your code or is it a typo here only? Also, what format is your dates saved as and what is the format of $fullDate?
    – s_ha_dum
    Commented Nov 10, 2013 at 15:23
  • The single quote is a typo. Thanks for pointing me out. The date is saved in YYMMDD. The date filtering is working fine, but I get all past events back even when the field 'activiteit_verslag' is not filled in Commented Nov 10, 2013 at 15:31
  • 1
    Do you mean YYYYMMDD? Otherwise I am surprised the filtering works.
    – s_ha_dum
    Commented Nov 10, 2013 at 16:04
  • Yes I mean YYYYYYMMDD, sorry for that Commented Nov 10, 2013 at 16:16
  • 1
    I'm not totally sure, but might this just be a difference between key EXISTS and empty value? Commented Nov 10, 2013 at 16:19

1 Answer 1


The problem appears to be, per the string of comments in the question, that the activiteit_verslag key is set for many, or all posts, even if the value is empty/NULL. EXISTS only checks the presence of the key. The value is not considered, so too much is returned.

There are a couple of things I can see for you to do.

The first is to use != instead of EXISTS.

  'key' => 'activiteit_verslag',
  'value' => '',
  'compare' => '!='

That should work but only if your values are consistently set to an empty string and only if the key is set for all posts.

WP_Query/WP_Meta_Query is not capable of complex logic like you would need to check whether the key is not set or set but empty, though you can write filters to make it work... but don't. The resulting SQL will become increasingly less efficient as the logic becomes more complicated.

The correct solution is to back up and alter how your data is saved. If you have an empty value for that activiteit_verslag key, then delete the key. If you do that then your query above should work (obviously untested but I am pretty sure) and you will probably have the most efficient possible query for this circumstance.

I realize that you are using Advanced Custom Fields to do your work for you but @ialocin thinks the plugin may have filters that would let you alter its behavior. I don't use ACF (or many other plugins because other people don't think like me and are therefore wrong :) ), so I don't know what you can or can't do with its hooks, but the Core hook save_post should do in a pinch.

Proof of concept:

  function($pid) {
    $empty = get_post_meta($pid,'delete_me',true);
    if (empty($empty)) {
  • Tkank you, I understand it. The problem is that I cannot alter the way my data is saved because I am using the advanced custom fields plugin. And when no activities_verslag is filled in, it returns null. Maybe a solution (not the nicest one) is to do a query on past dates and then process the data with another custom loop Commented Nov 12, 2013 at 9:47
  • ACF can be customized easily, take a look at the documentation, especially ACF's own action and filter hooks. A good start would likely be the acf/save_post hook. I'm pretty certain, that what s_ha_dum has suggested can be done with ACF in use. @MisterMelotte Commented Nov 12, 2013 at 14:12
  • @MisterMelotte : see the edit.
    – s_ha_dum
    Commented Nov 12, 2013 at 14:41
  • Of course you are right about not needing the plugin and ergo the plugin hook. But you know, not everyone can be as good as your are. BTW wouldn't this be like, why use wordpress, if you can do it on your own. ;) Besides that, I was suggesting the plugin hooks having in mind, that there possibly might be a way to prevent ACF saving empty fields in the first place, which would be nicer then saving and removing. One last addition, it might be necessary to give the function that removes the post meta a higher priority, just to make sure the sequence is always correct. Commented Nov 12, 2013 at 15:40
  • @ialocin : that "not thinking like me" part was a joke :) It is not worth doing everything yourself (though I do maintain that many plugins are far more trouble than they are worth). The first thing I said in the edit is that the plugin might have hooks to alter its behavior, as you suggested. That is, I am agreeing with you. I don't have the time to dig through that massive plugin to work our how to actually do it though and offered a fallback solution if all else fails.
    – s_ha_dum
    Commented Nov 12, 2013 at 15:50

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.