Check for the existence of custom field

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

Related posts

1 comment

  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','');
        }
      }
    );
    

Comments are closed.