I am happily using this bit of code to auto-tag posts by author (thank you again cybmeta):
add_action( 'save_post', 'add_authors_name');
function add_authors_name( $post_id ) {
$post = get_post( $post_id );
$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 );
}
}
This is doing exactly what I need, but I soon noticed this function also applies to pages. I would like to exclude pages and a custom post type I created called 'profiles' to from this function. I tried to piece together another bit of code I found but I think I'm just an idiot and didn't put it together properly. After the top bit of code I added:
<?php
add_filter( 'pre_add_authors_name', 'zk_exclude_pages' );
function zk_exclude_pages( $query ) {
if ( $query->is_search ) {
$query->set( 'post_type', array( 'post' ) );
}
return $query;
}
I was thinking that I would be able to make this apply to only posts this way (thus excluding "pages" and "profiles"). But it didn't work.
I'm sorry if there are several problems in my code. I'm just learning. Thank you for your help.