(Advanced Custom Fields Plugin) How can I set true/false field value to always be false instead of NULL

1.4k Views Asked by At

I’ve been trying to sort first page products with true/false field. As it is true the product is supposed to appear on first page. I used this meta_query to show all products

‘meta_query’ => array(
‘relation’ => ‘OR’,
array(
‘key’ => ‘show_on_first_page’,
‘compare’ => ‘EXISTS’,
),
array(
‘key’ => ‘show_on_first_page’,
‘compare’ => ‘NOT EXISTS’,
)
),

This mixes up all the products but if acf true/false field has been checked once it shows the products in the right order. Is there a way to set acf true/false field to be false globally on default if it is set to NULL?

1

There are 1 best solutions below

4
Andy2021 On
add_filter('acf/load_field/key=field_60ca0457d6821', 'my_acf_load_field');

function my_acf_load_field( $field ) {
        $field['default_value'] = 0;
        return $field;
    }

Where key=field_60ca0457d6821 update to the key of the true/false field in your case.

This will need adding to functions.php.