0

I have a custom post type called 'community' and I am trying to automatically generate the tag 'community' upon publishing anything from that custom post type. In layman's terms, a kind of 'if this is a community post type, then automatically tag this post as 'community'.' I want this to apply to the community custom post type only, and not the default wordpress post type.

I have tried a few iterations of the following code (which I have cannibalized from a previous inquiry) but could not get it to work:

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

$valid_post_types = array( 'community' );

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

       wp_set_post_terms( 'community', 'post_tag', true );

    }

}

}

I know this is a sloppy piece of coding, so I apologize for that. I have tried to google this issue but anything that remotely resembles the term 'auto tagging' returns results from plugins that automatically generate tags based on content (not based on post type).

I also think that the above bit of code is also set to apply to 'posts' as well, but I'm unsure of how to specify 'community' only. I have used the Map Cap plugin to map out the capabilities and control the 'community' type, so would I have to change the 'save-post' bit here to 'save_community'?

Sorry for all of the confusion. Thank you for your help.

1 Answer 1

0

Referenced from https://codex.wordpress.org/Post_Status_Transitions#.7Bstatus.7D_.7Bpost_type.7D_Hook the hook named "{status}_{post_type} Hook". This hooks is fired, when custom post type "community" is giving the status "publish" = is published. Hook will give post_id and post object as parameters to the callback function. And wp_set_post_terms, requires first parameter as post ID.

This should work:

    add_action('publish_community', 'community_post_type_tagging', 10, 2);
    function community_post_type_tagging($post_id, $post){
        wp_set_post_terms( $post_id, 'community', 'post_tag', true )    
    }
0

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.