I have a taxonomy eg. authors. I want to create a custom UI for. I think integrating with a Meta Box together with other post meta will make things easier for the user. Also I think a jQuery UI AutoComplete (Multiple comma separated values) is more appropriate

So I will need to know how I can "update_post_taxonomy". I suppose I will need to

  • if taxonomy does not exists
    • create it
  • if post does not have the taxonomy term
    • add it
  • remove terms that are not specified by user

I am sure there are functions for this?


The function you are looking for is wp_set_post_terms()

which can create the terms if they don't exists and you can specify if to appended or replace existing terms. so you can remove all none user terms.

as for

if post does not have the taxonomy term , add it

you can use wp_get_post_terms() to check if the post has any terms of that taxonomy and if not then you can create and add using wp_set_post_terms() once again

  • wp_set_post_terms will check & add if terms exists for the post right? So I dont need to use wp_get_post_terms? Thats what I understand from the 1st part of your answer – JM at Work Jun 29 '11 at 8:14
  • wp_set_post_terms will set the term and if it doesn't exists it will create it and then set it, wp_get_post_terms you can use to check if any term is set to that post – Bainternet Jun 29 '11 at 9:00
  • What am I supposed to pass into the $tags argument? Term name or slug? Or will WordPress automatically resolve it? – JM at Work Jun 30 '11 at 8:07
  • should work with both as far as i know – Bainternet Jun 30 '11 at 8:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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