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
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
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:
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:
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.