Home Forums Backend Issues (wp-admin) Automatically select option in relationship field if there is only one option


Automatically select option in relationship field if there is only one option

  • Hey!
    On my website I have relationship field where many options are available to choose. In case that an user with a specific role is logged in and wants to create a new post (where the relationship field is placed) the user only sees options in that relationship fields which belongs to his user account. This is already working fine. But in case that there is only one option available in the relationship field, this option needs to be selected automatically. Before a few days, it was working on ACF 4.9.X. After upgrading that page to the current version of ACF this mechanic is not working anymore. I don’t know how it was done but I tried to find appropriate solution for the current version of ACF.

    One obvious change regarding to the version 4.9.X is that the available options in the relationship field are only loaded via ajax when the field becomes visible in viewport.

    So what I need is probably a JS event, which is called when the ajax call of the relationship field is done and the result is placed in the frontend. Then I would check if there is only one item in the list of available options and afterwards I would select it. I would try this by using a “.click()” trigger on the first result but if there is a more charming solution for selecting the only available option, I would be happy if you could show it to me :-).

    I hope you can help me at this point!



Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.