Home Forums General Issues strategy for replacing Text Field with Taxonomy term value selection?


strategy for replacing Text Field with Taxonomy term value selection?

  • Hi,

    I’ve taken over a site where our client administrators are having to type the name of a taxomony term value into a text field on a custom post

    Unfortunately it’s likely that they misspell or use other versions of the correct value

    It should really be a drop down taxonomy term selector .. ie ACF field type = Taxonomy.

    let’s say the field is called subject and one of the values is “Book Shop”, sometimes this might be added as eg “Bookshop” or “Book-shop” and this is breaking some of the template logic

    There are 1000+ existing records and I could do a search & replace, but again it should really just be a select box based on the Taxonomy Terms

    however if I change the field type to correct this for future posts, it’s going to break the old posts

    What would be the best strategy for fixing this going forward whilst dealing with the old data?

    I don’t preferably want to change it to a normal select box with fixed values (so it stores as a single string in the db anyway as per…

    Really I want it as a term value selector so any changes to the actual original Taxomomy Term are actually directly updated via the reference

    However these field types are therefore incompatible.

    Can I for instance create a new field with the same name (again let’s say subject) but a different type… and then adjust my logic to account for the old text versions and the new term value reference versions? (and if old posts are updated it will overwrite with the new value?)

    Or would it be better to create an entirely new field with a different name going forward and then adjust the template to check for both (prioritising the new one first)

    thanks for any suggestions

  • Make a new taxonomy picker field with another name for example subject_picker and adapt your logic if subject_picker field has a value do new logic if not do old logic.

    and also maybe create terms by the old text inputs with a job. (looping all your posts, getting the field and creating term (wp_insert_term()).

    then do a job and set value to all the old terms for the new subject_picker field.

  • Hi @laju

    yes that does seem to be the easiest solution.

    I think it would maybe be possible to run a bulk script to update the value in-place with the term ID (ie in the same field) and then switch the field type after, but I’ll need to check exactly how the data is stored

    any other ideas also welcome!


  • i dont see any other idea.. if you want to refactor and use a different field type you need to bulk convert the old values to match your new

  • I kind-of agree with the idea of using a different field. My usual method of making changes like this is to.

    1. Create a new field
    2. Create an acf/prepare_field filter that will dynamically set the value of the new field based on the old field whenever a post is edited.
    3. create an acf/save_post action that deletes old field value when a post is updated.
    4. use an acf/prepare_field filter to remove the old field so that it can no longer be edited
    5. Create logic in my template that first looks for a value in the new field, if that exists then use the new field and if a value does not exist then fallback to using the old field.
Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.