3

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 ?

3 Answers 3

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.

2
  • It's weird to me that you can set a default value but it can't be read. Using the Loco Translate plugin, for my case I need to set the value to true for some language and false for some other, so I should add some more condition to your trick, and maybe update in the future if I add a language, it would have been much simplier if I could just correctly read the default value
    – Cédric
    Nov 29, 2022 at 9:42
  • 3
    Or just $display_sorting = get_field('display_sorting') ?? true;, then you do not need to test for null, as it will not ever be. Nov 29, 2022 at 19:40
5

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.

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.