Support

Account

Home Forums General Issues Assign a taxonomy term to items in Post Object

Solving

Assign a taxonomy term to items in Post Object

  • Hi

    I have a custom post type called ‘vendor’. That post type has an associated taxonomy called “filtered” and that taxonomy has a term called “enabled”. In my options page I have a post object field that lets user to select any post in the above post type. I would like to then dynamically assign the taxonomy term “enabled” to every post selected in that post object field. It should be dynamic, meaning if a post gets removed from that list, the term also gets removed from the said post.

    I know it sounds like a weird way of doing things, but I’m first letting user select which items are highlighted on the site, but I want to make it an easier UIX so that they don’t need to go also assign a term for every post separately as well, but rather do that based on their selection in the post object field.

    I’m not exactly sure where to even start with this so any help would be greatly appreciated, thanks!

  • Anyone have any ideas where to go about this?

    I have some kind of idea how to use wp_set_object_terms() to set the terms, but having trouble gathering all posts in the Post Object field and then adding the term into them. And don’t really have any idea how to make the dynamic aspect of removing the term if posts gets removed from the Post Object field.

    Thanks

  • You need to create an acf/save_post action. You need to set the priority of your action < 10 so that it runs before ACF saves the value.

    You can get the new value of the field from $_POST['acf']['field_XXXXXXX'] where field_XXXXXXX is the field key of your relationship field.

    You can get the old value from the database at this point using get_field('relationship_field_name', $post_id, false), the 3rd argument in the call tells acf not to format the value

    In both cases the value will be an array of post IDs. You then need to compare the two arrays and add or remove terms from each post based on that comparison using wp_add_object_terms().

  • Thank you for the tips. I’ll try and get this figured out!

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.