Support

Account

Home Forums Backend Issues (wp-admin) Dynamically populating checkboxes from Options page, but with preselected option

Solved

Dynamically populating checkboxes from Options page, but with preselected option

  • Hope I’m describing this accurately!

    I’ve already checked out the info on the “Dynamically populate a select field’s choices” page and got it to work successfully. Yay!

    So I have a text field on the Options Page where my client can add more options which are pulled into a custom post type as checkboxes. So far so good. However, when a new option is added on the Options Page, this appears on each page as an unselected checkbox. This means if a new option is added that needs to be checked for every page, you have to go into each page and manually check it.

    My question is this – is it possible for new options added to the Options Page to appear on each page as pre-checked checkbox items by default?

    Thanks in advance

  • For new posts created? Yes. In the same function that you are using to set the choices you can also set the ‘default_value’ to include an array of “Values” and set them all as default values.

    For existing posts? This would be more difficult. I’m not even sure it would be possible. What I’m thinking is: What if the admin has edited this post and un-selected the new value. In this case you would not want to add the value to this page. It would be impossible to check for this condition.

    When a new value is added you could update all posts to have this new value, but it would be problematic if you have many posts that need to be update. It is not scale-able. You can create. You could create an acf/update_value filter for this field on the options page https://www.advancedcustomfields.com/resources/acf-update_value/. In this filter you could compare the old values of the field with the new values of the field and if there is something added you could update any existing posts. You would need to get every post of the post type, loop through them, get the value of the checkbox field, add the new values to the list and then update the field. This is where it will fall apart if you have many posts in the system because it will time out.

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

You must be logged in to reply to this topic.