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