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

How to add automatically keyword to taxonomies when a post published, and assign them to the post
for example i have in my post edition, a custom meta box, when you complete this input, a function should generate a group of keywords in background , and i want these keywords are automatically adding to a specific custom taxonomy in that post when it published,
this is possible?
i try with

wp_set_object_terms

and nothing working great,
thx

sorry for my worst english

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x