Adding An Author Tag To Posts Automatically

Reading your comment, what you want is to create an author archive using tags and customize the author archive pages. You don’t need tags or categories for that. There is a built-in author archive, by default the URL is in the format yoursite.com/author/username. That URL will show all posts by author and will use the template hierarchy as you can see here. To customize the author archive you can use the template author.php; if you want to customize the archive for a specific author you can use the template author-{username}.php or author-{usernid}.php.

Also, you can pick up all posts by author using WP_Query, get_posts, etc, for custom queries and loops.

Although I don’t get the point of tagging posts with author name, here a working and tested code:

add_action( 'save_post', 'add_authors_name', 10, 2);
function add_authors_name( $post_id, $post ) {

    // Check the post type to apply only to satandard posts.
    // Bypass if the $post is a revision, auto-draft or deleted
    if( $post->post_type == 'post' ) {

        $post_author = $post->post_author;  // returns the Author ID
        // get the author's WP_User object so we can get the Author Name
        $post_author_obj = get_userdata( $post_author );
        $post_author_name = $post_author_obj->first_name . ' ' . $post_author_obj->last_name;

        if( ! has_term( $post_author_name, 'post_tag', $post ) ) {

            wp_set_post_terms( $post_id, $post_author_name, 'post_tag', true );

        }


    }


}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)