WordPress meta_query Null Issue
Recently I came upon a bit of an issue while working on a custom pre_get_posts filter. This filter uses meta_query to only return posts with a custom field (an Advanced Custom Fields Date field) that either doesn’t exist or is greater than or equal to a certain value. To achieve this I was using the following code inside of the filter:
For a while I thought this was a perfect solution, during my tests it was working well, but alas, we all make mistakes. What I didn’t account for was when the custom Date field was set and then later removed. As it turned out this doesn’t actually null the field in WordPress, just make it blank. This meant that those posts that had the custom Date field removed would no longer show up in archive pages, etc. To combat this, I recommend adding another array that checks if the value is blank. My final (and working) code ended up being this:
If anyone has a better suggestion or solution to this problem, I’m open to hearing it. Just give me a shout on Twitter (@jckprry).