Meta query based on ACF field – how to handle posts that have no value for this field

Question

I have a custom post type and I am working on building a filtering system based on the person’s age. I have a custom field called max_age and I am building a meta query to filter posts based on this field. Some of these posts have a value for max_age and some of them don’t. For the ones that don’t, I would like to always show those posts in the results regardless of what age they select in the filtering system. Here is what my meta query looks like:

'meta_query' => array(
    'key' => 'max_age',
    'value' => $_POST['selected-age'],
    'type'    => 'NUMERIC',
    'compare'  => '>='
);

This filter is mostly working, in that if you select a max age in the filter, it returns all posts with a max age that is less than or equal to the selected age. However, I am not sure how to adjust this query so that it also returns all posts that have no value in the max_age field. Is it possible to do this without specifying a value in all posts?

0
user13286 4 months 2023-11-15T12:09:22-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse