How to add automatically keyword to taxonomies when a post published, and assign them to the post

You would use the save_post hook, in your hooked function use wp_insert_term as described here:

http://codex.wordpress.org/Function_Reference/wp_insert_term

Then use wp_set_object_terms on the post to assignt he taxonomy term you just created as follows:

http://codex.wordpress.org/Function_Reference/wp_set_object_terms

for example:

function my_save($post_id) {
    wp_insert_term( 'bannanapost', 'fruit');
    wp_set_object_terms( $post_id, 'bannanapost', 'fruit', true )
}
add_action('save_post','my_save');

The above code, placed in functions.php of your theme, would add the term ‘bannanapost’ to each post when saved, in the fruit taxonomy

Leave a Comment