0

I have been researching how to automatically tag a post by author, so if "Peter Parker" authors a post, that post would automatically show up with a tag "Peter Parker." This is because I will have a multi-author blog with custom post types picked up by various blog rolls, and don't want to leave it up to users to remember to tag themselves in every single post.

I came across this thread that seemed to be asking the same thing as I'm doing now and ended up with the following code:

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 );
 }

I put this in functions.php and it broke my site. I had to login to the hosting company and reset the file through ftp.

I then came across this thread that also provided an acceptable (albeit more messy) approach that would automatically assign authors to specific categories. That lead me to a plugin that someone replied with but that was from 3 years ago and the plugin no longer worked when I tried to install/activate it.

I then searched more on google but only found a freelancer.com link that was requesting this kind of plugin be made.

I would appreciate your help. I've tried to do a good amount of research into this but ended up breaking my site or hit dead ends. Thank you.

edit: As I was able to explain thanks to cybmeta's helpful prompts, I do indeed want to eventually try my hand at customizing author.php, but right now I do not have enough know how to even formulate questions acceptable for stackexchange. This auto-tagging of authors is the last piece of the puzzle for me getting on with my site without coding anything else, so I would be grateful for assistance with that. Thank you so much for your help so far, cybmeta.

  • 2
    A post's author is already assigned to the post (I see you know that as you are using $post->post_author). I don't get the point of creating a tag with the author name. What is the purpose of doing it? – cybmeta Apr 20 '15 at 16:26
  • I want to make customized author profiles with through the 'pages' part of wordpress. Each author would have an individual 'blog roll' content element, but right now this element only picks up categories and/or tags (i.e. there is no option to pick up 'all posts by x author'). So, I want to make the author's name a tag itself so I can easily pick up everything that author posts in their blog roll. – zk87 Apr 20 '15 at 23:30
1

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 );

        }


    }


}
  • Thank you for this. I apprecite your time to go over my inquiry and reply. If I can speak honestly, I don't think this route is viable for me because there are a few more customization I am looking to do to the author archive pages including adding social media icons and separate bio box. Also, the default author template displays quite poorly on mobile at the moment. – zk87 Apr 21 '15 at 8:09
  • My solution to all of this was to create custom pages and redirect all calls for individual author pages to these newly built pages. To implement all of the changes I want, I would have to dive further into customizing the php which I have no idea how to do (and which I've already broken my site with multiple times). I am learning php on an ongoing basis, and maybe redirecting ot pages is not viable in the long term, but pages are something I am much more familiar with and can implement now. – zk87 Apr 21 '15 at 8:12
  • I have spent a long time going down both routes (customizing the php which I have no in depth knowledge of vs. redirecting to pages) and I have come to the point where this is the only single piece of coding I would have to do to get the redirection to pages route working (i.e. having the blog roll pick up individual posts by author). I really have tried to put the work in to find the solution myself and I feel bad for even asking this above inquiry. I am just more familiar and confident with using the visual layout editor at this point in my coding education. – zk87 Apr 21 '15 at 8:16
  • So basically I feel like I'm imposing by even making my original inquiry without a deeper understanding of php and that I should just know how to do it myself. And to make the kind of author profile I want, I would have to do a few more customizations and make more inquiries (which I don't even feel I'm justified in doing because I don't have the php knowledge to even make an attempt at these other customizations). – zk87 Apr 21 '15 at 8:21
  • 1
    thank you so much for this. i also learned what an XY problem is as well now, so thank you for teaching me about that. it sounds very much like what happened here and i will try to avoid it in the future. you are an amazingly helpful person. i really appreciate you taking the time and being patient! – zk87 Apr 21 '15 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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