Tell us what you are trying to do?
Update the checkboxes field to specific values via php.
Is there any documentation that you are following?
None that is adequate, that explain the exact datatypes expeced by the update_post_metadata() funciton
Is there a similar example that we can see?
This is essentially the same question as posted below. https://toolset.com/forums/topic/update-checkboxes-checked-state-via-php/
My code is like this:
` $first_entry = array(
"wpcf-fields-checkboxes-option-6e20d12e07fc5ce114f9456756fc5b5c-1" => array("Wiederholungen"),
"wpcf-fields-checkboxes-option-25d9ca35050dd66242dcbd169bf65168-1" => 0,
"wpcf-fields-checkboxes-option-3adf42187da5b3898cb12e302d297cba-1" => 0,
"wpcf-fields-checkboxes-option-53b36d5ad031b3f2cb343d899c3092aa-1" => 0
);
update_post_meta( $post_id, 'wpcf-the-showing-type' , $first_entry );`
Which then ends up looking like this in the database
`a:4:{s:64:"wpcf-fields-checkboxes-option-6e20d12e07fc5ce114f9456756fc5b5c-1";i:0;s:64:"wpcf-fields-checkboxes-option-25d9ca35050dd66242dcbd169bf65168-1";i:0;s:64:"wpcf-fields-checkboxes-option-3adf42187da5b3898cb12e302d297cba-1";i:0;s:64:"wpcf-fields-checkboxes-option-53b36d5ad031b3f2cb343d899c3092aa-1";i:0;}`
/**
* 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 );
}
}
}
Where:
- Replace 99999 with your original post ID you want to update the checkboxes field
- Replace 'event-format' with your original checkboxes custom field slug
I used the code above, no errors but the field does not save.
This is what gets populated in the db
a:4:{s:64:"wpcf-fields-checkboxes-option-6e20d12e07fc5ce114f9456756fc5b5c-1";i:0;s:64:"wpcf-fields-checkboxes-option-25d9ca35050dd66242dcbd169bf65168-1";i:0;s:64:"wpcf-fields-checkboxes-option-3adf42187da5b3898cb12e302d297cba-1";i:0;s:64:"wpcf-fields-checkboxes-option-53b36d5ad031b3f2cb343d899c3092aa-1";i:0;}
The DB gets populated but wrong values are inserted into the db
Array
(
[wpcf-fields-checkboxes-option-6e20d12e07fc5ce114f9456756fc5b5c-1] => 0
[wpcf-fields-checkboxes-option-25d9ca35050dd66242dcbd169bf65168-1] => 0
[wpcf-fields-checkboxes-option-3adf42187da5b3898cb12e302d297cba-1] => 0
[wpcf-fields-checkboxes-option-53b36d5ad031b3f2cb343d899c3092aa-1] => 0
)
Can you please share admin access details and where you are trying to use the code I shared.
What option exactly you want to check using PHP?
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I have set the next reply to private which means only you and I have access to it.
Thank you for sharing that information. But when I try to login to FTP using the access details you shared, I can only see index.php file. Can you please grant access so that I can access the functions.php file of your theme.
Sorry - that will not work. Can you please add the code I shared to "Custom Code" section as in that section I can add/modify the code but if I try to edit in functions.php file if something goes wrong it will result in that the site will be broken and I will not be able to make any changes.