Skip Navigation

[Resolved] problem with getting wpcf- field

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
#1162142

I am trying to: I can insert and update post/postmeta/user/usermeta. BUT can not get it to work to insert into meta_key wpcf-tilknytning in wp_postmeta the meta_value from wp_usermeta meta_key wpcf-tilknyttetafdeling

$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' => $user->wpcf-tilknyttetafdeling,   //THE PROBLEM
    			),
		    'post_category' => array( 3)
		);
		// Insert my_post i database.
		wp_insert_post( $post );

Link to a page where the issue can be seen:
page is role restricted
I expected to see:
wp_postmeta meta_key 'wpcf-tilknytning' with a value lets say 2
Instead, I got: 0

#1162696

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - as I understand, you are looking to get the user meta value for user meta field "tilknyttetafdeling" and wanted to assign it to 'wpcf-tilknytning' field. If yes, could you please try the following code and try to resolve your issue:

$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 );
#1162827

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); 
}

New threads created by Minesh and linked to this one are listed below:

https://toolset.com/forums/topic/split-problem-with-getting-wpcf-field-cred-field-not-saved/

#1162834

Minesh
Supporter

Languages: English (English )

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

Glad to know that your original issue is resolved.

As per our support policy, we entertain only one question per ticket. This will help other users searching on the forum and help us to write correct ticket resolution summery. Thank you for understanding. Please feel free to resolve this ticket and we will continue with your new question with the ticket I split.

#1164126

My issue is resolved now. Thank you!