Skip Navigation

[Resolved] change radio button to multi select checkboxes without losing current data

This thread is resolved. Here is a description of the problem and solution.

Problem:

How should I go about changing the radio to multi select checkboxes without losing current data and maintaining the copy function between post types?

Solution:

There aren't such kind of built-in feature within Toolset plugins, both your requests need custom codes:

https://toolset.com/forums/topic/change-radio-button-to-multi-select-checkboxes-without-losing-current-data/#post-1607929

Relevant Documentation:

This support ticket is created 4 years, 8 months ago. 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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 4 replies, has 2 voices.

Last updated by taih 4 years, 8 months ago.

Assisted by: Luo Yang.

Author
Posts
#1607345

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

#1607439

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.

#1607443

Here is the original convo about the radio button filter.

https://toolset.com/forums/topic/filtering-results-by-radio-button-on-a-child-post-field/

thanks so much!

#1607929
1.jpg

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.

#1611413

Thank you for the comprehensiveness of your answer. My issue is resolved now. Thank you!