How to convert comma separated checkbox values into arrays

Question

I am trying to save comma separated Checkbox field values into arrays and save into Toolset based checkbox custom fields.

I am currently using “super forms” form with frontend posting addon to create post from frontend post and when I allow the user to select checkbox options then due to comma-separated structure in SuperForms field, values are not getting saved in “Toolset” custom checkbox field. So I created a custom function that triggers as soon as Superform’s frontend posting addon saves the post. But my function is not working.

Please help me to fix it.

function f4d_convert_metadata( $attr ) {
    // CHANGE THIS LIST TO ANY META DATA YOU NEED TO CONVERT
    $meta_keys = array( 'wpcf-recent-works', 'wpcf-skills' );
    // DO NOT CHANGE BELOW CODE
    $post_id = $attr['post_id'];
    foreach($meta_keys as $meta_key){
        // Grab meta value
        $meta_value = get_post_meta( $post_id, $meta_key, true );
        // Convert to Array
        $meta_value = explode(',', $meta_value);
        // Save it as array
        update_post_meta( $post_id, $meta_key, $meta_value );
    }
}
add_action('super_front_end_posting_after_insert_post_action', 'f4d_convert_metadata', 10, 1);
0
, , , , Manish Bajpai 3 years 2020-06-06T11:11:08-05:00 0 Answers 100 views 0

Leave an answer

Browse
Browse