0

This is code I set on function.php I made it to have original author's nickname as custom value.

function data_nickname( $post_id, $post, $update ) {
$data_nickname = get_post_meta($post->ID, 'data_nickname', true);
    if(empty($data_nickname)):{
        $author_ID = $post->post_author;
        $data_nickname = get_the_author_meta('nickname',$author_ID);
        update_post_meta( $post_id, 'data_nickname', $data_nickname, true );}
    endif;
}
add_action( 'wp_insert_post', 'data_nickname', 10, 3 );

Author's nickname is sometimes kept as custom value as I expected, but also nickname of modified author is kept most of time.

I only like to keep original author's nickname. Not modified author.

How I can correct my code?

3
  • Can you please clarify distinction of "original author's nickname" and "modified author's nickname"?
    – Caleb
    Jun 7, 2023 at 14:35
  • Original author is person who made article first. Modified author is person who update the article later. Thank you
    – cwhiro
    Jun 7, 2023 at 15:18
  • wouldn't it be easier to track the user ID? That way when the original author changes their nickname it gets updated, otherwise you could have temporary usernames and deadnames stored in post meta, and fixing it would take a lot of manual work
    – Tom J Nowell
    Jun 7, 2023 at 18:49

1 Answer 1

1

After review, I would expect your code to work properly: to only save the creating author's nickname, and not override it later.

Here's some code to try with slight improvements and redundancies (tested):

function data_nickname( $post_id, $post, $update ) {
    // If the post is being updated, bail.
    if ( $update ) {
        return;
    }

    $author_ID = $post->post_author;
    $nickname  = get_the_author_meta( 'nickname', $author_ID );

    // Only set post meta if it doesn't already exist.
    add_post_meta( $post_id, 'data_nickname', $nickname, true );
}
add_action( 'wp_insert_post', 'data_nickname', 10, 3 );

I recommend changing the post meta key to something more clear than data_nickname, like original_authors_nickname. In five years and possibly a different developer, it will then be more clear what it's purpose is.

8
  • Thank you for your advise about name of meta key.
    – cwhiro
    Jun 7, 2023 at 18:09
  • Thank you for your advise. Looks like something wrong with your code. Your code is sophisticated, and I can not find what part I should correct.
    – cwhiro
    Jun 7, 2023 at 18:29
  • Sorry, fixed my code.
    – Caleb
    Jun 7, 2023 at 18:31
  • Thank you. It stopped showing error now, but does not produce meta key, and nickname is not saved.
    – cwhiro
    Jun 7, 2023 at 18:40
  • I tested in a fresh WordPress install, and it worked properly. Can you please test in a fresh WordPress install and a default theme?
    – Caleb
    Jun 7, 2023 at 18:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.