Skip Navigation

[Resolved] Duplicate post but not Author

This support ticket is created 6 years, 6 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 2 replies, has 2 voices.

Last updated by Nashaat 6 years, 6 months ago.

Assisted by: Nigel.

Author
Posts
#953706

I am using following code to duplicate woocommerce products.

add_action('cred_save_data_713', 'duplicate_post', 10, 2);
function duplicate_post($post_id, $form_data) {
    // get data of original post
    $post = get_post( $form_data['container_id'], ARRAY_A );
  	// update Post Status to PENDING
  	$post['post_status'] = 'pending';
    // update the new post with this data
    $post['ID'] = $post_id;
    $post['post_title'] = 'Copy of ' . $post['post_title'];
    wp_update_post( $post );
    // get fields of original post
    $fields = get_post_custom( $form_data['container_id'] );
    // update the new post with these fields
    foreach ($fields as $key => $values) {
        foreach ($values as $value) {   
            add_post_meta($post_id, $key, $value, false);
        }
    }
}

when duplicating a product, the post author is also duplicated. what i want to achieve is to duplicate the post but the Author should be the current user ho has duplicated the post. how can i modify this with my code?

#954331

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

You can use get_current_user_id() to get the id of the current user and use that to update the post_author before creating the duplicate. Your edited code would look something like this:

add_action('cred_save_data_713', 'duplicate_post', 10, 2);
function duplicate_post($post_id, $form_data) {
    // get data of original post
    $post = get_post( $form_data['container_id'], ARRAY_A );
    // update Post Status to PENDING
    $post['post_status'] = 'pending';

    // set author as current user
    $author_id = get_current_user_id();
    $post['post_author'] = $author_id;

    // update the new post with this data
    $post['ID'] = $post_id;
    $post['post_title'] = 'Copy of ' . $post['post_title'];
    wp_update_post( $post );
    // get fields of original post
    $fields = get_post_custom( $form_data['container_id'] );
    // update the new post with these fields
    foreach ($fields as $key => $values) {
        foreach ($values as $value) {   
            add_post_meta($post_id, $key, $value, false);
        }
    }
}
#954431

Works perfectly! Thank you Nigel