Tell us what you are trying to do?
I have some custom fields which are in the multiple checkbox type. I want to update these fields programmatically using PHP. I've tried to use the following:
update_post_meta($postData['post_id'], "wpcf-".$slug, $value);
This works for standard Types fields, but I can't get it to work for multiple checkbox fields.
In the above example, $value is an array.
Here is more of the code to give more context. You can see I've tried to output when it's comma separated to help with troubleshooting.
// Update custom fields using Toolset Types or update_post_meta
foreach ($postData['custom_fields'] as $slug => $value) {
// Convert boolean values
if (strtolower($value) === 'yes') {
$value = 1;
} elseif (strtolower($value) === 'no') {
$value = 0;
} elseif (strpos($value, ',') !== false) { // Check if the value contains a comma
// If the value contains a comma, it's a list of values for multiple checkboxes
$value = array_map('trim', explode(',', $value)); // Split the string into an array, trimming whitespace
echo "<br />MULTIPLE VALUES: $slug - ";print_r($value);echo "<br /><br />";
}
// Assuming $slug is the custom field key
update_post_meta($postData['post_id'], "wpcf-".$slug, $value);
Is there any documentation that you are following?
I'm struggling to find documentation on this particular situation.
Hello. Thank you for contacting the Toolset support.
You can add the following code to "Custom Code" section offered by Toolset and add a new code snippet (do not forget to activate the code snippet) and add the following code to it:
/**
* Unofficial API function to check/uncheck checkboxes options
*
* @param int $post_id
* @param string $field // slug of checkboxes field
* @param string $option // title of checkbox option to manipulate
* @param string $action : 'check' | 'uncheck' | 'value' (default, returns current value)
*
* Important: assumes recommended checkboxes setting of save nothing to database when unchecked
*/
function ts_checkboxes( $post_id, $field, $option, $action = 'value' ){
if ( isset($post_id) && isset($field) && isset($option) ){
$field_settings = types_get_field( $field );
$field_options = $field_settings['data']['options'];
// Locate the option key
$key = array_search( $option, array_column( $field_options, 'title' ) );
$keys = array_keys( $field_options );
$option_key = $keys[$key];
// Get the current post meta value
$meta = get_post_meta( $post_id, 'wpcf-'.$field, true );
if ( !is_array($meta) ){
$meta = [];
}
// If action = 'value' just return the value
if ( $action == 'value' && isset( $meta[$option_key] ) ){
return $meta[$option_key][0];
} else {
// Remove the existing key if it exists (i.e. uncheck)
// because recommended setting is to save nothing when unchecked
unset( $meta[$option_key] );
// If $checked == true then add back the key + value
if ( $action == 'check' ){
$meta[$option_key] = array($field_options[$option_key]['set_value']);
}
update_post_meta( $post_id, 'wpcf-'.$field, $meta );
}
}
}
You can use the above function to checkmark the:
[php]
/// set the same option as checked
global $post;
ts_checkboxes( $post->ID, 'destinations', 'Facebook', 'check' );
///// set the same option as unchecked
global $post;
ts_checkboxes( $post->ID, 'destinations', 'Facebook', 'uncheck' );
/code>