plugins - TrueFalse ACF field returning null by default even if default value is true

admin2025-01-07  8

I made a true/false ACF field called display_sorting. It appears that it returns null no matter if I set the default value or not.

<?php var_dump(get_field('display_sorting'));?>
returns :
[...].php:26:null

If I update the value from the page the field appears, the value will be correctly set to true or false.

<?php var_dump(get_field('display_sorting'));?>
will return this time :
[...].php:26:false
or
[...].php:26:true

My problem is that I would like to set the default value to true (which is supposed to be the option I try here) so I don't have to set manually the value on x pages, but currently the default value isn't properly read.

Am I missing something ?

I made a true/false ACF field called display_sorting. It appears that it returns null no matter if I set the default value or not.

<?php var_dump(get_field('display_sorting'));?>
returns :
[...].php:26:null

If I update the value from the page the field appears, the value will be correctly set to true or false.

<?php var_dump(get_field('display_sorting'));?>
will return this time :
[...].php:26:false
or
[...].php:26:true

My problem is that I would like to set the default value to true (which is supposed to be the option I try here) so I don't have to set manually the value on x pages, but currently the default value isn't properly read.

Am I missing something ?

Share Improve this question edited Nov 29, 2022 at 9:19 Cédric asked Nov 29, 2022 at 8:53 CédricCédric 1411 silver badge6 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 5

As Harit Panchal mentioned.
Creating a new ACF field, true/false in this case, and setting a default value for it, wont automatically set this value for every existing post, page, term, etc, it's connected to.

What you can do in this case is this.
We know that non existing field value is null, you want that the default will be true, and instead of going one by one and updating the field you can so this condition.

// set the value to a variable as we will use it more than once
$display_sorting = get_field('display_sorting');

if ($display_sorting === true || is_null($display_sorting)) {
    
} else {
    
}

Because we want the default to be true and we know that non existing meta returns null, we created a logic that would consider both cases as a truthy value.

You will need to update every existing post otherwise, you will get NULL every time even if you set the default value to TRUE.

You can bulk edit/update altogether.

The answers so far explain how to fix this but not why it happens. The issue exists because when you created the existing items the default was null. This means that the existing items have a value in the display_sorting field. When you change the default the code doesn't know whether or not to update the existing items all it knows is that the default for new items is "true". It isn't clear whether or not null is still a valid value for items that were created prior to the new default. The principle of "least surprise" indicates that if there's a choice of doing something and doing nothing with no further information a system should do nothing. The code doesn't even know whether you have something that specifically handles the old nulls.

To illustrate slightly differently, imagine this is a price field where the previous default was $1 (i.e. not implicitly null), somethings are currently correctly priced at $1, and you're updating your default price to $2 for inflation. Do you suddenly want everything priced at $1 to be priced at $2? $1 is still a valid price for some items, would you like to review which items will double in price before mass updating them? I'd want to know which items were changing.

If you know that all of your null values are to be updated to true then you have to specifically do that yourself. The alternative is far worse than the current solution.

For future reference, you can also use the acf/load_value filter to return the default value of the field if the value is null.

add_filter('acf/load_value/name=display_sorting',  'load_display_sorting', 10, 3);

public function load_display_sorting($value, $post_id, $field) {
    return $value ?? $field['default_value'];
}

Reference: https://www.advancedcustomfields.com/resources/acf-load_value/


Conversely, you could do this in a more all-encompassing way, but I'm not sure about the overhead of this because it would happen on EVERY single field. However, it would force all fields to return their default if they're null.

add_filter('acf/load_value',  [$this, 'load_default_value'], 10, 3);

public function load_default_value($value, $post_id, $field) {
    return $value ?? $field['default_value'];
}

Open to suggestions to make this better.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736260769a688.html

最新回复(0)