Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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')

   )
);

$pastActivities=query_posts($arg);
share|improve this question
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 Nov 10 '13 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 –  Mister Melotte Nov 10 '13 at 15:31
1  
Do you mean YYYYMMDD? Otherwise I am surprised the filtering works. –  s_ha_dum Nov 10 '13 at 16:04
    
Yes I mean YYYYYYMMDD, sorry for that –  Mister Melotte Nov 10 '13 at 16:16
1  
I'm not totally sure, but might this just be a difference between key EXISTS and empty value? –  ialocin Nov 10 '13 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.

array( 
  '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:

add_action(
  'save_post',
  function($pid) {
    $empty = get_post_meta($pid,'delete_me',true);
    if (empty($empty)) {
      delete_post_meta($pid,'delete_me','');
    }
  }
);
share|improve this answer
    
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 –  Mister Melotte Nov 12 '13 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 –  ialocin Nov 12 '13 at 14:12
    
@MisterMelotte : see the edit. –  s_ha_dum Nov 12 '13 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. –  ialocin Nov 12 '13 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 Nov 12 '13 at 15:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.