Navigation überspringen

[Gelöst] Copy content into new post but change the author

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

Problem: I would like to use a cred_save_data hook to modify a post's author to be the current User.

Solution:
Add the current User's ID to the post's 'post_author' argument:

add_action('cred_save_data_34', '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 the new post with this data
    $post['ID'] = $post_id;
    $post['post_title'] = 'Copy of ' . $post['post_title'];
     
    // -- add this line to modify the post author --
    $post['post_author'] = get_current_user_id();
     
    wp_update_post( $post );
}

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://codex.wordpress.org/Function_Reference/wp_update_post

This support ticket is created vor 6 Jahren, 11 Monaten. 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Dieses Thema enthält 2 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von Daniel vor 6 Jahren, 10 Monaten.

Assistiert von: Christian Cox.

Author
Artikel
#614898

Hello,

I successfully get a function to clone / copy content of an older record.
https://toolset.com/forums/topic/copy-content-but-clear-some-fields/

But it also copies the old author. It would be great, if the author is set to the current user?

Best, Daniel

#615026

Hi, you can add the current User's ID into the post's post_author field during the cred_save_data hook. See below:

add_action('cred_save_data_34', '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 the new post with this data
    $post['ID'] = $post_id;
    $post['post_title'] = 'Copy of ' . $post['post_title'];
    
    // -- add this line to modify the post author --
    $post['post_author'] = get_current_user_id();
    
    wp_update_post( $post );
    ...
#615773

thanks a lot for this solution. This worked fine for me.