Support

Account

Home Forums ACF PRO Add new term + term meta on Post screen

Helping

Add new term + term meta on Post screen

  • Hi – I have an “Author” taxonomy where the authors are entered in in first + last format, ie Jane Doe. I have set up custom fields attached to this taxonomy for First Name and Last Name, and that works great.

    But, while admins can create + attach new Author terms on a Post or Book or whatever CPT we set, they have to then go back over to the main Author taxonomy page to find the term and enter in the first/last term metadata custom fields.

    Can anyone think of a way to both create a new term + add termmeta to that term on a Post or other CPT screen?

    I’ve been brainstorming and have thought of two options, trying to figure out the best user experience + clean/fast code:

    1 – Use native ACF taxonomy field to add term(s), save draft of Post (necessary I think to create the term), then somehow give admins a way to enter + save custom meta for the terms they’ve created from the same Post screen.

    2 – Give admins two options: one where they’d choose from already existing Author terms, another where they’d enter in custom first + last name fields. If the latter, programmatically create the term + add term meta, then attach the new term to the Post.

    Thought I’d throw this out to see if anyone had already attacked this problem & had suggestions before I dive into coding. Thanks for your thoughts!

  • There is a plugin that will let you do this for relationship fields, but I cannot find anything available to allow doing this with terms.

    This is not something that can be done in ACF without a lot of work. For example, if you wanted to do the modal thing for editing.

    If I had to do this what I would probably do is the following…. I don’t have code and there would be a lot of it, so I can’t really provide it without actually building this.

    1) I would create a fields, I would probably use a “Group” field. This field would be conditional on the taxonomy field having any value. Alternately you could have an “update the term” true false field and and make the group conditional on this field.

    2) In the group field I would add the fields that need to be updated in the term. I would make these fields required, but this would be more difficult because you wouldn’t know if the term is new or not, if the term already has these values you’d probably not want to update them, but more on this later.

    3) I would create an acf/prepare_field filter. In this filter I would get the value from the taxonomy field and I would use this to get the values from the term and populate the group field with these values if they exist.

    3) I would then add an acf/save_post filter. In this filter I would get the value from the taxonomy field and the fields that need to be updated in the term and I would update the term with these values. After updating the term I would delete the values from group field values from the post, they will be populated again when the post is reloaded (see #3)

    4) I would create custom JavaScript that would detect a change in the taxonomy field, make an AJAX request and populate the values of the group field based on the term selected. I have some examples of this here, but nothing related to doing these things based on a taxonomy field https://github.com/Hube2/acf-dynamic-ajax-select-example

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

The topic ‘Add new term + term meta on Post screen’ is closed to new replies.