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 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.
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 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.
Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked Apr 20, 2015 at 13:57 zk87zk87 1191 gold badge3 silver badges9 bronze badges 2 |1 Answer
Reset to default 1Reading 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/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 );
}
}
}
$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 Commented Apr 20, 2015 at 16:26