Skip Navigation

[Resolved] multiple checkboxes values always showed

This support ticket is created 6 years, 1 month 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
- 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 5 replies, has 2 voices.

Last updated by Nigel 6 years, 1 month ago.

Assisted by: Nigel.

Author
Posts
#1120826

I am trying to:

Display a value (image tag) only when a multiple checkbox option is selected. Different options selected have different values.

Instead, I got:

I always get the value of the selected state.

here is the code:

[types field='wine-experience-languages' option='0' state='checked']
<img class="iconFlag tooltips" title="Català" src="hidden link" alt="Català" />
[/types]
[types field='wine-experience-languages' option='0' state='unchecked']  [/types]

and similar code for the other options. It used to work and now not anymore.

#1121415

Nigel
Supporter

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

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

Hi Gian Luca

I tested this and found that somewhere between Types 2 and Types 3 it broke.

Specifically, it doesn't work when your checkboxes custom field settings are set to save zero to the database when an option is not checked. If the option is to save nothing, then this works correctly.

Saving zero has been the source of a number of problems and it is now officially discouraged, but that doesn't help you where you have an existing site that has posts with zero saved for unchecked options.

I'm escalating this so that it can be fixed or a workaround created for you, and I'll let you know when I have something to report.

#1121474

What about some MySQL query to set all 0 values to NULL ?

#1121506

Nigel
Supporter

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

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

A colleague helped another user with a similar issue (which relates to using checkboxes fields as Views filters, which isn't working when zero is saved to the database).

He produced the following script, which you can try on your own site.

1. Make a backup before running the script.

If you are not familiar with it, Types 3.1 added the ability to add custom code to your site without the need to edit your theme's functions.php etc, which you can read about here: https://toolset.com/documentation/adding-custom-code/

This feature lets you add a code snippet and run it once only ("Run now"), which is what you need here.

2. Add the code below to a custom code snippet on your site, first editing the variables at the top of the script for the post type the checkboxes field is assigned to, as well as the meta keys of the checkboxes custom fields (don't forget the 'wpcf-' prefix).
3. Run the code once
4. Confirm on some sample posts that the checkboxes options appear to be correctly set
5. Confirm that the report issue is no longer present

Here is the code

<?php
/**
 *  One time code to 'save nothing' for existing unchecked checkboxes options
 *  currently saved as zero
**/
function change_checkboxes_value_zero_to_nothing() {

	/**
	 * This part has to be adapted to your specific case 
	 * Please make sure to cahnge post type slug and field slugs as you need them
	 * $affected_post_type 				 = The slug of the post type in question
	 * $affected_custom_fields_meta_keys = comma separated list of Custom Fields, which are Checkboxes, and belong to the above type, and save 0 when nothing is checked
	 */

	$affected_post_type = 'artist';
    $affected_custom_fields_meta_keys = "'wpcf-instrument','wpcf-music-genres'"; //you may add more Checkboxes Fields belonging to the Post Type set above

	/**
	 * This part has not to be touched.
	 * It gets all posts IDs of our type
	 */
    $affected_post_ids = get_posts(array(
        'numberposts' => -1, // get all posts.
        'post_type' => $affected_post_type,
        'fields' => 'ids', // Only get post IDs
    ));

	/**
	 * This part has not to be touched.
	 * It loops over each post, gets it's checked checkbox value for each custom field and updates the field by saving only the checked values, nothing if 0
	 */
    if ($affected_post_ids) {
        global $wpdb;
        $affected_post_ids = implode(',', $affected_post_ids);
        $sql = "SELECT meta_key, meta_value, post_id FROM $wpdb->postmeta WHERE post_id IN ($affected_post_ids) and meta_key IN ($affected_custom_fields_meta_keys)";
        $postmeta = $wpdb->get_results($sql, ARRAY_A);
        if (!empty($postmeta)) {
            foreach ($postmeta as $mKey => $mVal) {
                $serialized_meta = unserialize($mVal['meta_value']);
                $returned_options = [];
                if (!is_array($serialized_meta)) continue;
                foreach ($serialized_meta as $key => $value) {
                    if (is_array($value)) $returned_options[$key] = $value;
                }

                /**
                 * Here the actual update to the Database happens.
				 * If you first want to make sure the program gets the correct data, you can uncomment this var_dumps and comment the below update_post_meta()
				 */
				// var_dump($postmeta[$mKey]['post_id']);
                // var_dump($postmeta[$mKey]['meta_key']);
                // var_dump($returned_options);
                update_post_meta($postmeta[$mKey]['post_id'], $postmeta[$mKey]['meta_key'], $returned_options);
            }
        }
    }
}

change_checkboxes_value_zero_to_nothing();

Let me know how you get on. Don't forget that backup.

#1121507

Okay. I'll try that. I suppose I need to uncheck the option to 'save 0 in the database' for unchecked options, first of all.

#1121513

Nigel
Supporter

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

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

Yes, you'll need to do that so that all new posts are saved correctly.