Skip Navigation

[Closed] Converting radio to checkbox in bulk

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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 15 replies, has 2 voices.

Last updated by catherineB 1 year, 2 months ago.

Assisted by: Nigel.

Author
Posts
#2639597

Tell us what you are trying to do?
Convert old radio button data into new checkbox data.
I'm used to do it for taxonomies and it works well. But for radio-to-checkbox, it seems harder

Radio csv options: direct, meme-etage, ascenseur, plateforme, rampe-interieure
Checkbox csv options (based on extract of the actual data that some posts have):
- a:1:{s:64:"wpcf-fields-checkboxes-option-3ff9e737ef29002ba9c6544ee0970018-1";a:1:{i:0;s:6:"Direct";}}
- a:1:{s:64:"wpcf-fields-checkboxes-option-c7600c4dd2093d25ee19570d5080e8ed-1";a:1:{i:0;s:17:"Rampe intérieure";}}
etc.

I've completed the whole csv file with the new a:1.... data and imported it. I did not receive any error messages, but nothing was updated in the posts.

Is there anything I must do differently for checkbod custom fields?

What is the link to your site?
hidden link

#2639707

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Catherine

Checkboxes fields are stored in the database in... a very particular and opaque way.

You can't simply try to reproduce the format you see for what is stored in wp_postmeta for some other checkboxes field.

If you are importing posts from a CSV file there really is no alternative but to use a premium import plugin (https://toolset.com/course-lesson/how-to-import-content-into-wordpress-using-csv/).

If you already have content on your site made with radio fields that you want to "convert" to use checkboxes fields, one option might be to write a script that iterates over all of the posts with saved values for the radio field and use the function shared below to set the equivalent options of a checkboxes field as checked.

/**
 * 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
			// if ( is_array($meta) ) {
				unset( $meta[$option_key] );
			// } else {
			// 	$meta = [];
			// }
			
			// 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 );		
		}
    }
}

Examples of how you might use that unofficial API function would be:

// 1. Get the value of the option "Second checkbox" of a checkboxes field "Available options" from the current post:
global $post;
$value = ts_checkboxes( $post->ID, 'available-options', 'Second checkbox' );
 
// 2. set the same option as checked
global $post;
ts_checkboxes( $post->ID, 'available-options', 'Second checkbox', 'check' );
 
// 3. set the same option as unchecked
global $post;
ts_checkboxes( $post->ID, 'available-options', 'Second checkbox', 'uncheck' );

That could be the basis for a script to convert your radio field content, but if you are looking to import new content you would need to use one of the paid import plugins.

#2639849

Thank you Nigel for your answer.

As I have many different fields to convert (first major update in years), I think it will be easier to pay the plugin and just do the import.

So I bought the Ultimate CSV Importer plugin and tried to import my csv. But I'm stuck lol !! (I always face unexpected bugs hahah)

I've setted up the custom field settings but when I click on "Continue", it says it is mandatory to add the language_code data

But when I export in CSV with my usual plugin, I do not have this data. So I tried exporting with the ultimate plugin, and realised maybe only 5% of the posts do have this data

Could I corrupt my post info if I add this data to all? Or if I say EN when it's actually the FR version?

Catherine 🙂

#2640187

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Catherine

Let's confirm what it is you are doing here, before offering any advice.

You have a mature existing site which, amongst other things, has a bunch of radio custom fields (only one option can be selected) that you would like to convert to checkboxes custom fields (multiple options can be chosen).

The site is multilingual, and uses WPML, and the content is (mostly? somewhat? all?) translated to a second language.

The intention is to export the content (at least, the posts these custom fields belong to) to CSV files.

You will then make changes to the site to set up the checkboxes fields (you'll either add them with different slugs to the existing fields, or you will delete the old fields and their content and then set up new checkboxes fields with the same slugs).

And then you will import the CSV files and, presto!, your checkboxes fields will be populated with the data from the old radio fields.

(Naturally, you will have a backup before you make any lasting changes in case you need to revert!)

And now the issue arises that you are expected to set the language information when importing content?

I'm guessing this is because the Ultimate CSV Importer plugin recognises that you have WPML installed (I haven't ever used it in the context of WPML, so I'm not sure), and it makes it a requirement. (I'm surprised by that. I would expect if no language information is provided then it would assume the site default language.)

You say when you exported the content with the Ultimate plugin only ~5% of posts have language set?

That doesn't sound right.

If you look at some of the examples in the CSV file where the language setting is missing and then you go into the back end of your website and you edit the post is the language for the post not set?

You can go to WPML > Support and follow the Troubleshooting link, where you'll see a button to "Set language information", which will add language information to posts where it is missing.

Then try exporting your CSV file again...

#2640717

Good morning Nigel,

I tried few things.
1) Exporting through Ultimate CSV is a nightmare.
- I can't customize the export, so I get ALL data, which transform my 3K line csv file in a 30 K line csv files, with 90% of lines being WYSIWYG text files.
- It make it impossible to work with the file

2) I tried to only add the language_code slug in a new column, with no data in, so the system would see that the field is there and let me move forward. As only <5 % of the data is set in this field, I shouldn't make a mess importing empty data
- The system DID recognize the field and let me move on,
- BUT, it then required the "Translated post title" field (and content)
- My normal CSV export do not link EN / FR post titles. It export all in individual post IDs / lines
- The Ultimate CSV export tool give me an impossible csv file to work wit
- So I'm back to case 1, I can't import the new data

Grrrrr!!!! Haha

Catherine 🙂

#2640721

I will specify that ~70% of the 3K posts have a manually translated EN title, but the remain 30% are now complete duplicates (auto-duplicated when the form is submitted)

#2640731

Ok.
So I tried with a "empty" language_code column, AND an "empty" translated_post_title
I succeeded to go through the steps and import the file, but the data was not converted.....

From what you know, did it worked for other people?

I'll write to the technical support of the plugin

#2640737

Here is a copy of the message I sent to the plugin's team

Good morning,

I've bought your plugin few days ago.

I have a website with 3000 posts that I want to convert radio fields to checkboxes. I work with toolset, that's their team that told me about Ultmate CSV import plugin.

I'm having problems
1) The plugin requires the language_code data
- The thing is that my normal CSV plugin do not export such field. So I exported data with your plugin, and saw that < 5% of lines were populated with the language_code data
- So I tried to simply add the column, with no data in and tried to reimport

2) It appears that the plugins also requires the translated_post_title field
- My normal CSV plugin do not link EN / FR post titles. It export all in individual post IDs / lines

3) Exporting with your plugin transform my 3K lines CSV file in a 30K line CSV files, where 90% of the lines are separated sequences of WYSIWYG fields. And as the post title is such a field, it is lost in the 30 K lines

4) I tried to import with both language_code and translated_post_title fields (with empty data)
- The system did go throught the whole process and did import the file
- BUT, nothing changed in the posts, the data was not converted / uploaded

Q: is it normal? Am I missing something?
Q: if I have empty data for both language fields, could I corrupt / errase data (in any of both langages?)

Thanks 🙂

Catherine

#2640975

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

I'm not familiar with using the Ultimate CSV import plugin in the context of WPML, so I'm not sure what to say.

I assume that it is requiring language-related fields because it recognises that you have WPML installed on your site and it is designed to import multilingual content.

Here's a thought: how about disabling your WPML plugins beforehand so that it doesn't know you have WPML and a multilingual site, and it just treats posts as posts without any language information associated?

#2643259

Hi NIgel,

Sorry for late reply.

As I have more than 3000 pages per language, I prefer to not play with plugins hahahah. I don't want to create unecessary bugs! 😀

I tried the Custom Field version of the plugin, which doesn't recognized language data.

I've bee able to proceed with the import without having to provide language data, but I got the same problem that the data did not upload / update

So I'm still with the customer service.

catherine 🙂

#2643295
toolset-radio-checkbox-import-5.JPG
toolset-radio-checkbox-import-4.JPG
toolset-radio-checkbox-import-3.JPG
toolset-radio-checkbox-import-2.JPG
toolset-radio-checkbox-import-1.JPG

Hi Nigel,

Some updates.
I realized that the fields were updated, but "not completely"
- Fields were updated in the database, because I can see the data with a CSV export (picture 1)
- Fields were updated in the databse, because I see them when I show them in a dynamic tab / form field (picture 2)

This said
- It won't show up in the post meta when I open the post "edit page" (picture 3)
- And it seems to not show on the post template page (picture 4 and 5 - it should show "Direct 2" under Accès intérieur / Direct)

It's like a "link" wasn't updated throught the import

Post URL : hidden link
Template URL : hidden link
Page with dynamic tab / form field : hidden link

Any thoughts?

CAtherine 🙂

#2643603

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Screenshot 2023-09-13 at 12.15.19.png
Screenshot 2023-09-13 at 12.13.55.png

Am I right in thinking you exported data which includes the radio custom field "Intérieur", and that before importing back the data, you added a column which would be for the new checkboxes custom field "Intérieur de la maison"?

Looking at your settings for this new custom field, I can see how this would be problematic.

In the screenshots you can see that your radio custom field stores values for the selected option in text form (e.g. "direct").

But your checkboxes custom field stores "1" for all options.

So what are you putting in the column in the CSV file for this field to specify which value should be checked? (I don't know how the import plugin would handle a scenario where you wanted multiple values selected, maybe a comma-separated list of values; you'd need to clarify with them.)

I suspect what you would need to do is to change the settings for the checkboxes field so that the options save text values to the database that correspond to the existing values for your radio field. Then you can simply copy across the column for the existing radio field to the new checkboxes field before importing, when—hopefully!—it will detect the checked options and set them up accordingly.

#2643633

Good morning Nigel 🙂

In order to do the conversion, I've first exported the data in CSV. Then I kept only 3 columns (post ID, interieur, acces-interieur).

So I would have
22356 direct a:0:{} (which is the equivalent of empty value)

In some cases I had previously updated the checkbox option in withing the post file, so I would have data

a:1:{s:64:"wpcf-fields-checkboxes-option-3ff9e737ef29002ba9c6544ee0970018-1";a:1:{i:0;s:6:"Direct";})

So I've simply copy-pasted this value in all the post ID whose "interieur" field was marked as "direct"

I did the same with all other options
a:1:{s:64:"wpcf-fields-checkboxes-option-536d00b958750d86c1f5a90e23f4f959-1";a:1:{i:0;s:9:"Ascenseur";}}
a:1:{s:64:"wpcf-fields-checkboxes-option-3ff9e737ef29002ba9c6544ee0970018-1";a:1:{i:0;s:6:"Direct";}}
a:1:{s:64:"wpcf-fields-checkboxes-option-ccea8a5c5ebe157dd3bb3f565b3454fe-1";a:1:{i:0;s:12:"Même étage";}}
a:1:{s:64:"wpcf-fields-checkboxes-option-38cb732cdbdc90b4f22d145b3d7ab6c3-1";a:1:{i:0;s:10:"Plateforme";}}
a:1:{s:64:"wpcf-fields-checkboxes-option-c7600c4dd2093d25ee19570d5080e8ed-1";a:1:{i:0;s:17:"Rampe intérieure";}}

In the case of multiple selections (ones I've edited directly in WP post file) it would show
Direct + rampe intérieure :
a:2:{s:64:"wpcf-fields-checkboxes-option-3ff9e737ef29002ba9c6544ee0970018-1";a:1:{i:0;s:6:"Direct";}s:64:"wpcf-fields-checkboxes-option-c7600c4dd2093d25ee19570d5080e8ed-1";a:1:{i:0;s:17:"Rampe intérieure";}}

Direct + plateforme
a:2:{s:64:"wpcf-fields-checkboxes-option-3ff9e737ef29002ba9c6544ee0970018-1";a:1:{i:0;s:6:"Direct";}s:64:"wpcf-fields-checkboxes-option-38cb732cdbdc90b4f22d145b3d7ab6c3-1";a:1:{i:0;s:10:"Plateforme";}}

Direct + même étage
a:2:{s:64:"wpcf-fields-checkboxes-option-3ff9e737ef29002ba9c6544ee0970018-1";a:1:{i:0;s:6:"Direct";}s:64:"wpcf-fields-checkboxes-option-ccea8a5c5ebe157dd3bb3f565b3454fe-1";a:1:{i:0;s:12:"Même étage";}}

Direct + ascenseur
a:2:{s:64:"wpcf-fields-checkboxes-option-3ff9e737ef29002ba9c6544ee0970018-1";a:1:{i:0;s:6:"Direct";}s:64:"wpcf-fields-checkboxes-option-536d00b958750d86c1f5a90e23f4f959-1";a:1:{i:0;s:9:"Ascenseur";}}

Direct + meme étage + ascenseur (which is a mistake because it can't work, but still haha)
a:3:{s:64:"wpcf-fields-checkboxes-option-3ff9e737ef29002ba9c6544ee0970018-1";a:1:{i:0;s:6:"Direct";}s:64:"wpcf-fields-checkboxes-option-ccea8a5c5ebe157dd3bb3f565b3454fe-1";a:1:{i:0;s:12:"Même étage";}s:64:"wpcf-fields-checkboxes-option-536d00b958750d86c1f5a90e23f4f959-1";a:1:{i:0;s:9:"Ascenseur";}}

Direc + rampe intérieure
a:2:{s:64:"wpcf-fields-checkboxes-option-3ff9e737ef29002ba9c6544ee0970018-1";a:1:{i:0;s:6:"Direct";}s:64:"wpcf-fields-checkboxes-option-c7600c4dd2093d25ee19570d5080e8ed-1";a:1:{i:0;s:17:"Rampe intérieure";}}

So you can see that the first number (a:X:) represents the number of elements "checked"

Catherine 🙂
N.B. I'll try to change the "1" to something else

#2643637

Oh.... I juste realized you were looking for the wrong checkbox field. THe one you took the picture from is for the housing, but I have the issue with the locations

Radio field: interieur
Checkbox field: acces-interieur

Both are in the ACCÈS - LIEUX custom field group.

#2643905

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Screenshot 2023-09-14 at 09.33.00.png

OK, thanks for sharing the output of the CSV file examples.

From that I would say that the plugin does not in fact support Types checkboxes fields, it is simply exporting the raw database value (and Types stores checkboxes fields in an admittedly awkward format that means the raw output in a CSV file is virtually meaningless).

What you are aiming to do could in theory work—even if it is a necessarily ugly workaround—except when I check the contents of your database and look at the imported data I can see where it is going wrong.

See the screenshot, showing examples of the entries for the wpcf-acces-interieur fields.

Note how the very first line—which must be an example you created by manually checking the checkbox in the post editor—is correct, and looks like:

a:1:{s:64:"wpcf-fields-checkboxes-option-3ff9e737ef29002ba9c6544ee0970018-1";a:1:{i:0;s:6:"Direct";}…

But you can see where the imported rows are different:

s:101:"a:1:{s:64:"wpcf-fields-checkboxes-option-3ff9e737ef29002ba9c6544ee0970018-1";a:1:{i:0;s:6:"Di…

It's the same, except the whole content is wrapped in "s:101:".

These are serialized arrays, where s101 means what follows is a string 101 characters long, where a:1 means what follows is an array with one element etc. (You can use a tool like hidden link to convert them into something sensible.)

And the import tool is taking the original values and re-encoding them again to specify that they are a string 101 characters long, unnecessarily.

I'm afraid you'd have to discuss this with the import plugin support, you don't want that to happen. (Or ask them about proper support for the checkboxes field.)

If that leads to a dead end, an alternative would involve you adding a PHP callback function to process the value being imported which runs every time you import a value for that field.

WP All Import Pro has this, perhaps the Ultimate CSV Importer plugin has the same, in which case can you get more details and I can help you with that. (It might be a better route to go down anyway.)

The topic ‘[Closed] Converting radio to checkbox in bulk’ is closed to new replies.