Skip Navigation

[Resolved] Split: problem with getting wpcf- field – CRED field not saved

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

Problem:
User custom field not saved with Toolset form when creating the user and insert the post

Solution:
User was using the wrong ID to fetch the user meta. I've corrected it to $post_id.

You can find the proposed solution, in this case with the following reply:
https://toolset.com/forums/topic/split-problem-with-getting-wpcf-field-cred-field-not-saved/#post-1163552

Relevant Documentation:

This support ticket is created 5 years, 11 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

Author
Posts
#1162835

Maybe the right way, but now i get 1 in meta_value field, so i think you solved my problem.
Do you know if cred_save_data is not saving meta_post before and thats the problem why the value now are 1?

add_action('cred_save_data', 'after_save_data_for_form',30,2);
function after_save_data_for_form($post_id, $form_data)
{
    //some code here
    switch($form_data['id']) {
    case "something":
        // ...
        break;
    case 450:
    	global $wpdb;
    	//get user
    	$user = get_user_by( 'id', $post_id);
		$user_id = get_current_user_id();
		$post = array(
            'post_title'    => $user->first_name . $user->last_name,
            'post_type'    => 'medarbejder',
            'post_content'  => 'Info om medarbejder '. $post_id,
            'post_status'   => 'publish',
            'post_author'   => $post_id,
            'post_parent'   => $user_id,
            'comment_status' => 'closed',
            'ping_status' => 'closed',
            'meta_input'   => array(
                '_wp_page_template' => 'default',
                '_wp_old_date' => '2018-12-04',
                'wpcf-tilknytning' => get_user_meta($user_id,'wpcf-tilknyttetafdeling',true),   //THE PROBLEM
                ),
            'post_category' => array( 3)
        );
        // Insert my_post i database.
        wp_insert_post( $post );
		//$wpdb->insert('wp_posts',$my_post);
		
		// hent den oprettede post.
		$posts = get_posts(array('post_author' => $post_id, 'post_type' => 'medarbejder'));
		foreach ($posts as $post) {
		    $title = get_the_title($post->ID);
		    $etid = $post->ID;
		    $permalink = get_permalink($post->ID);
		    break; //use this to limit to a single result
		}
		// valg af data for opdatering
        $columns = array(
        	'ID'			=> $etid,
		   	'guid' 			=> '<em><u>hidden link</u></em>'.$etid,
		    'post_content'  => 'medarbejder er nu opdateret' .$post_id.' og post ID er: '.$etid
		   //'post_parent'	=> $post->ID
		   
		);
		//wp_update_post($columns);
		$wpdb->update( $wpdb->posts, $columns, array( 'ID' => $etid ) ); 
        //wp_insert_post( $my_upost);

        break;
    default:
        //code to be executed if n is different from all labels;
    
	}
   
     //sleep(1); 
}
#1162837

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - I need to know the form is user type form which creates new user - correct? and you want to assign the value ID that belongs to newly created user - correct?

#1163165

Hell Minesh.
Yes you right. Actually my problem is that i need a post that is an copy of user fields or at least the one described below.
1: i have a create user form -id 450 - with

<div class="col-xs-6 form-group">
		<label>Tilknyttet afdeling</label>
		[cred_field field="tilknyttetafdeling" force_type="field" class="form-control" output="bootstrap"]
	</div> <!-- select box with values from 1 to 6-->

that field "tilknyttetafdeling i would like to put into wp_insert_post($post). But before it was 0 and after you corrected my code its 1.
I'm new to toolset so i thought that first cred_save_data then wp_insert_post then an $wpdb->update

Thanks for taking time

#1163552

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok - as I understand, it looks like you want to assign the value of "tilknyttetafdeling" field and add it that value using insert-post meta_input - correct?

Could you please try to use following code:

add_action('cred_save_data', 'after_save_data_for_form',30,2);
function after_save_data_for_form($post_id, $form_data){
    //some code here
    switch($form_data['id']) {
    case "something":
        // ...
        break;
    case 450:
        global $wpdb;
        //get user
        $user = get_user_by( 'id', $post_id);
        $user_id = get_current_user_id();
        $post = array(
            'post_title'    => $user->first_name . $user->last_name,
            'post_type'    => 'medarbejder',
            'post_content'  => 'Info om medarbejder '. $post_id,
            'post_status'   => 'publish',
            'post_author'   => $post_id,
            'post_parent'   => $user_id,
            'comment_status' => 'closed',
            'ping_status' => 'closed',
            'meta_input'   => array(
                '_wp_page_template' => 'default',
                '_wp_old_date' => '2018-12-04',
                'wpcf-tilknytning' => get_user_meta($post_id,'wpcf-tilknyttetafdeling',true),   //THE PROBLEM
                ),
            'post_category' => array( 3)
        );
        // Insert my_post i database.
        wp_insert_post( $post );
        //$wpdb->insert('wp_posts',$my_post);
         
        // hent den oprettede post.
        $posts = get_posts(array('post_author' => $post_id, 'post_type' => 'medarbejder'));
        foreach ($posts as $post) {
            $title = get_the_title($post->ID);
            $etid = $post->ID;
            $permalink = get_permalink($post->ID);
            break; //use this to limit to a single result
        }
        // valg af data for opdatering
        $columns = array(
            'ID'            => $etid,
            'guid'          => '<em><u>hidden link</u></em>'.$etid,
            'post_content'  => 'medarbejder er nu opdateret' .$post_id.' og post ID er: '.$etid
           //'post_parent'  => $post->ID
            
        );
        //wp_update_post($columns);
        $wpdb->update( $wpdb->posts, $columns, array( 'ID' => $etid ) ); 
        //wp_insert_post( $my_upost);
 
        break;
    default:
        //code to be executed if n is different from all labels;
     
    }
    
     //sleep(1); 
}
#1164119

My issue is resolved now. Thank you!