Skip Navigation

[Gelöst] Update Checkboxes Checked state via PHP

This support ticket is created vor 8 Jahre, 3 Monate. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 4 Antworten, has 2 Stimmen.

Last updated by julieM vor 8 Jahre, 3 Monate.

Assigned support staff: Ana.


HI Guys,

I am building a PHP script to update the custom fields created in types. I am able to update the single custom fields but I am having issues updating the checkboxes custom field group.

I get the state of the checkboxes from a separate API. Once i get the values of the checked boxes i would like to pass their checked state to a Types checkboxes custom field group.

I have been able to determine how to target each check option using:

require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
$field = wpcf_admin_fields_get_field('member-category');

But I am having trouble determining the best way to pass the checked state to the individual options in my checkboxes group.

I receive an array from the api that i can format how ever necessary to achieve the the transfer of its checked state to a Types Custom field group.

I found the location in the wp databse where the checkboxes custom fields are stored. The data is serialized and stored in one field.

Would be nice if there is an update_postmeta solution.

i can see the fields to be updated when i run this:


but when i select a field to update and pass its value using update postmeta this does not work

update_post_meta($post_id, 'wpcf-fields-checkboxes-option-0314e896b7fe92315d3f0eef2f1df5a3-1', 'Amateur');

Do i have to serialize my data and update db directly, or is there a better solution using update_postmeta or something similar.

Any help appreciated.

Thank you.



Dear Julie,

I would suggest you use get the checked option in a simple array like:

	$api_result = array( 'Amateur' );

then you can use this array to check against each option in the Types custom field

	$field = wpcf_admin_fields_get_field('member-category');
	$post_id = '12'; //the ID of the post to which to update the custom field
	$api_result = array( 'Amateur' );
	if (isset($field['data']['options'])){
	        $res = array();
		foreach ($field['data']['options'] as $key => $option){
			if (in_array($option['set_value'], $api_result)){
				$res[$key] = $option['set_value'];
	        update_post_meta( $post_id, 'wpcf-member-category' , $res );

In this example we are checking against the value to be saved: 'set_value' if you want to use the title then change to 'title'.

Please let me know if you are satisfied with my answer and if I can help you with any other questions you might have.



Hi Ana,

Thank you for your detailed response.

I am having the following issue when i implement you update code on my script. It does target the correct field in the wp db, but the data that it inserts is not formatted in a way that it can be displayed in the front end. It passes the data and its serialized but the data is not targeted for each individual option.

Result from after running update code above:

a:4:{i:0;s:12:"Professional";i:1;s:7:"Student";i:2;s:12:"Board Member";i:3;s:9:"Volunteer";}

Result when i use the wp front end to check the boxes manually:

a:22:{s:64:"wpcf-fields-checkboxes-option-0314e896b7fe92315d3f0eef2f1df5a3-1";i:0;s:64:"wpcf-fields-checkboxes-option-828339b3ea1c8b77e62735de3053a6fa-1";i:0;s:64:"wpcf-fields-checkboxes-option-b8821af576a4b9deb08fea09f36710ec-1";s:12:"Professional";s:64:"wpcf-fields-checkboxes-option-abc412bd3572e8191988063f824716ec-1";i:0;s:64:"wpcf-fields-checkboxes-option-4cc2797f9671b7977544ecf0145ce053-1";s:7:"Student";s:64:"wpcf-fields-checkboxes-option-ed235310ad839749d05d6d93ad4e4193-1";i:0;s:64:"wpcf-fields-checkboxes-option-dcf9843f1d738f914d4f968cc777f68b-1";i:0;s:64:"wpcf-fields-checkboxes-option-6ae3cadb1d905a58cb37b497f16f2788-1";i:0;s:64:"wpcf-fields-checkboxes-option-58636a462d0317d72a1c63f49e1a37eb-1";s:12:"Board Member";s:64:"wpcf-fields-checkboxes-option-c41574db8723feed25157392899c672a-1";s:9:"Volunteer";s:64:"wpcf-fields-checkboxes-option-b7aff3bd9ffd6342fc92bf04a960bf4c-1";i:0;s:64:"wpcf-fields-checkboxes-option-3c18c23b488ed51a2f25d760afc163ec-1";i:0;s:64:"wpcf-fields-checkboxes-option-36c320bec956a7fb14b06a61aef086ed-1";i:0;s:64:"wpcf-fields-checkboxes-option-992c79bea3eb5fec608a44b1c80aafc6-1";i:0;s:64:"wpcf-fields-checkboxes-option-87a12da921fd65a57e2f66c633bbb7b8-1";i:0;s:64:"wpcf-fields-checkboxes-option-e1185a5e46da96c90f312987d4d82824-1";i:0;s:64:"wpcf-fields-checkboxes-option-b72df57630c10ed2e6579bd61c4cc28b-1";i:0;s:64:"wpcf-fields-checkboxes-option-dd54f4eeddc01e6f2ac509b16c20912f-1";i:0;s:64:"wpcf-fields-checkboxes-option-3e1bd147bb08259428ecd4fd09c7f637-1";i:0;s:64:"wpcf-fields-checkboxes-option-3ae10e4d4bf014c9965fd4bd9e28bc5e-1";i:0;s:64:"wpcf-fields-checkboxes-option-722bd410069c8460351895ba4dbe58a7-1";i:0;s:64:"wpcf-fields-checkboxes-option-69cbdc839d3cb516a128b484f85f419d-1";i:0;}

So you can see that the specific options for each value are not being passed.

For the record I have checked "When unchecked, save 0 to the database" in case that makes any difference.



In case this helps,

here is the array i am passing to your php code as $api_result:

[0] => Professional
[1] => Student
[2] => Board Member
[3] => Volunteer


Ok I figured it out.

I formatted my api call to either give each value a '0' if unchecked or a return the value fof the checked box or boxes.

Then i got the name of the options in the Types checkboxes group using:

require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
$checked = wpcf_admin_fields_get_field('member-category');

later I created a array to send to "update_post_meta" that includes the returned values from my api call attached to the appropriate options from the types "get field" call.

$members_array = array(
"wpcf-fields-checkboxes-option-0314e896b7fe92315d3f0eef2f1df5a3-1" => $amateur,
"wpcf-fields-checkboxes-option-828339b3ea1c8b77e62735de3053a6fa-1" => $founding_member,
"wpcf-fields-checkboxes-option-b8821af576a4b9deb08fea09f36710ec-1" => $professional,
"wpcf-fields-checkboxes-option-abc412bd3572e8191988063f824716ec-1" => $semipro,

Ran Update post meta:

update_post_meta( $post_id, 'wpcf-member-category' , $members_array );

Worked! 🙂

Thanks for your help. If you can suggest any improvements please let me know.