0

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.

3
  • I've updated the answer in the original question.
    – cybmeta
    Commented Apr 23, 2015 at 6:01
  • dude, you are the best! is there any way i can leave you a testimonial or something like that? you have really gone out of your way, and i really appreciate it.
    – zk87
    Commented Apr 23, 2015 at 9:39
  • so yeah, the modified code indeed worked and stopped tagging pages, but also one consequence of this is that it doesn't pick up one of the other custom post types i have created. i have a post type called 'blog post' that is under the slug 'community'. i made this through the Types Plugin. i am guessing the next step is to register it in this functions along with the regular 'post' type. however, i'm unsure of how to exactly target it.
    – zk87
    Commented Apr 23, 2015 at 9:41

1 Answer 1

0

In save_post action you can access to the objet of the saved post and check the post type before tagging it:

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

    // The post types you want be tagged to $valid_post_types array
    $valid_post_types = array( 'post', 'community' );

    if( in_array( $post->post_type, $valid_post_types) ) {

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

        }

    }

}
2
  • 1
    dude, you are like the second coming of christ to me. thank you so much for this. i shall be singing songs of your valor for decades!
    – zk87
    Commented Apr 23, 2015 at 11:17
  • Hahaha, you are wellcome.
    – cybmeta
    Commented Apr 23, 2015 at 11:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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