• Resolved exsiteie

    (@exsiteie)


    Hi there,

    Just wondering is there a way to automatically add a post_tag of the author’s name to the post once “published” is clicked?

    EDIT: Forgot to mention, the post_tag has to go into the database (for retrieval) so meta keywords won’t help here.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter exsiteie

    (@exsiteie)

    Think I might be getting somewhere, but still not quite… anyone?

    add_action( 'save_post', 'add_authors_name', 10, 2);
    function add_authors_name( $post_id ) {
    $post_author = get_the_author($post_id);
    $post_author =  str_replace(' ', '-', $post_author);
    wp_set_post_terms( $post_id, "$post_author", 'post_tag', true );
    Thread Starter exsiteie

    (@exsiteie)

    Just in case anyone is looking for this in the future. This is the solution:

    Add to functions.php:

    add_action( 'save_post', 'add_authors_name');
     function add_authors_name( $post_id ) {
     global $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;
     wp_set_post_terms( $post_id, $post_author_name, 'post_tag', true );
     }
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Automatically Add Tag When Post is Published’ is closed to new replies.