Home Forums Front-end Issues Query posts by checkbox value NOT IN Array


Query posts by checkbox value NOT IN Array

  • I have a checkbox field type (multiple select).
    I know that checkbox array is saved as serialized in the database and that I should use 'meta_compare' => 'LIKE', as explained at Query posts by custom fields.
    I know also meta_compare operators.
    My problem is that I would like to set a meta_query like this:

    $homeQuery = new WP_Query( array(
    	'meta_key' => 'hide_from',
    	'meta_value' => 'home',
    	'meta_compare' => 'IS NOT'// or 'NOT LIKE'

    but it doesn’t work.

    Is there a solution?

    Thank you.

  • There is no solution just using ACF. If you want to store the value in the standard WP format then what you’ll need to do is create another custom field that is not part of ACF and when a values is saved to then update the value in the other field. For example:

    // priority of 20 to run after acf has updated value
    add_action('acf/save_post', 'convert_my_checkbox_to_standard', 20);
    function convert_my_checkbox_to_standard($post_id) {
      $values = get_field('my_check_box_field', $post_id);
      delete_post_meta($post_id, 'my_converted_checkbox_field');
      if (!count($value)) {
        // no values, bail early
      foreach ($values as $value) {
        add_post_meta($post_id, 'my_converted_checkbox_field', $value, false);

    With this in place you can then do your searches based on the converted field rather than the ACF field.

    If you’re interested I have created an entire plugin that’s designed for converting values in ACF fields to make them easier to use in queries

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Query posts by checkbox value NOT IN Array’ is closed to new replies.