Check for empty field in meta query and other

Hello there,
here is a quick one on how to check for an non/empty field.

In the visibility conditions we have the “empty” and “non empty” operators.

grafik

To achieve this with a meta query and other filters, we can compare the field with the comparison operator != (not equal) or = (equal) and leave the comparison value empty. In the following example, only non-empty objects are displayed, so it is the equivalent of the “Show visibility” condition above.

    array(
        'key' => 'acf_field',
        'compare' => '!=', // Check if the field is not equal to empty
        'value' => '',     // Empty value, as we're comparing to empty
    ),

Hope this helps :smiling_face:

Cheers

1 Like