Tell us what you are trying to do?
I have a custom field group where I have a Checkboxes. I have 4 options in that checkboxes whom I have given 1, 2, 3, 4 value. Now If I change any of those values, say I have updated 1 and made it 5, will the value get updated in the database for the already existing posts where I have used this custom field group?
Hi there,
No it will not. You will need to either do it manually or use a PHP code like the steps below to force an update for all the posts programmatically:
https://toolset.com/documentation/customizing-sites-using-php/updating-types-fields-using-php/
It is important that you create a complete backup of your database before doing so.
Thanks.
For the above scenario which code should I use? Can you give me the code?
Secondly, If I implement a custom search with that checkboxes, will it work properly after changing the checkbox values?
Hi,
Thanks for writing back.
An important point to note in this scenario is that if you'll change the option value(s) for the checkboxes type custom field, the new value won't be updated for the existing posts, but the checked and unchecked state will be maintained.
For example, suppose you have a checkboxes type custom field, with 4 options:
- Option 1 (value: 1)
- Option 2 (value: 1)
- Option 3 (value: 1)
- Option 4 (value: 1)
Next, you decided to change the option values so that they all have different numeric values:
- Option 1 (value: 1)
- Option 2 (value: 2)
- Option 3 (value: 3)
- Option 4 (value: 4)
After this change, any existing posts where the options were previously checked, they'll remain checked the same way. And since the search filtering uses the checked/unchecked state and not the actual values, it will continue to work as well.
The only drawback is that if you're showing the checkboxes values from this field, for the existing posts, they'll continue to show the old values and not the new ones. To update those, you'll have to open their individual post-edit screens and save them once, manually.
Unfortunately, the code example link that Christopher shared in the last reply won't work in this case. It will trigger when an individual post is edited either through the post edit screen or through the frontend form. But our requirement is different and we need to force the updation of new custom field values for the already existing posts, for which there is no built-in filter or hook available.
regards,
Waqar
If there is no filter or hook available, then can it be done through any MySQL command?
It can be done through direct changes to the database through MySQL, but, it will be complicated.
The values of the checked options are stored in the custom fields table, in a special serialized format, for example:
a:1:{s:64:"wpcf-fields-checkboxes-option-6a7a06a76a66278a70df7f547e5a894f-1";a:1:{i:0;s:1:"1";}}
You'll have to note down the serialized string generated in each possible combination of the checkbox options selection, both for the older option values and the newer option values.
Next, you'll need to perform a search and replace to find each instance of those strings for the older values with the strings for the newer values.
My issue is resolved now. Thank you!