Tell us what you are trying to do?
I have created a post field group with a radio button. It worked great for a year or so, but now they want to change the options to be a multi select checkbox instead of a radio button, and add a few options. I see I cant just change the input type from the edit field area.
There is also bit of a sticky part: The radio button field (gender) is part of a custom post type (narrators) that is referenced in another custom post type (samples) during a search. We need to be able to search by gender on both the narrators post types and the samples post types (on different pages). The previous help I received from toolset (ALWAYS top notch!!!) had given me a script/snippet to run that would copy the value of the radio button (gender) to the other post type (samples) when saving the gender of the narrator. ---pasting the snippet below:
How should I go about changing the radio to multi select checkboxes without losing current data and maintaining the copy function between post types?
--------------------------------------
function copy_parent_gender_field ( $post_id ) {
// when parent narrator post type is saved, copy its gender field to child audio posts
$post_type = get_post_type( $post_id );
if ( $post_type == 'narrator' ) {
$gender = get_post_meta( $post_id, 'wpcf-gender', true );
$audio_args = array(
'numberposts' => -1,
'post_type' => 'sample',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '_wpcf_belongs_narrator_id',
'value' => $post_id,
'compare' => '=',
'type' => 'NUMERIC'
)
)
);
$audios = get_posts( $audio_args );
foreach( $audios as $audio ) {
update_post_meta( $audio->ID, 'wpcf-gender-audiosample', $gender );
}
}
}
add_action( 'save_post', 'copy_parent_gender_field', 100 );
-----------------------------------------------------------------
What is the link to your site?
beeaudio.com
hidden link
hidden link
thank you so much for all you do!
cheers,
tai
I tried to create another field (both post types-narrators and samples) for the multiselect check boxes instead of changing the radio field. Then I copied the above snippet and swap out the "wpcf-gender-audiosample", and 'wpcf-gender' for the new fields, but it didn't copy/sync to the other post type.
Hello,
There aren't such kind of built-in feature within Toolset plugins, both your requests need custom codes.
Q1) changing the radio to multi select checkboxes without losing current data
You will need to use PHP codes to run batch update:
Find all existed "narrator" posts, run a loop
https://developer.wordpress.org/reference/classes/wp_query/
get radio field value
https://developer.wordpress.org/reference/functions/get_post_meta/
and set the checkboxes field value
https://developer.wordpress.org/reference/functions/update_post_meta/
Since custom checkboxes field stores value in array format, it is different from other custom field types, you will need to change the value into array format, see similar thread here:
https://toolset.com/forums/topic/update-checkboxes-checked-state-via-php/#post-235795
Q2) maintaining the copy function between post types
The custom PHP codes you mentioned above is using legacy post type relationships, and it needs this:
Setup a same checkboxes field in child post type "sample", for example, edit the post field group of post type "sample", click "Add new field" button, there is a button "Choose from previously created fields", see screenshot: 1.JPG
so after copy the field value, it should be able to keep the same value in both post types.
Thank you for the comprehensiveness of your answer. My issue is resolved now. Thank you!