Home Forums ACF PRO Reload the result of Relationship using other fields' value before submit


Reload the result of Relationship using other fields' value before submit

  • I am trying to reload the result of Relationship or Post Object field using other fields’ value before submit.
    So I wanna know the most simple way to do it.

    For example…
    (numbers are steps of operation)

    1: make 2 custom post_type
    – ‘campaign’
    – ‘news’

    2: add 2 ACF field to the post_type ‘campaign’
    – ‘start_date’: as datetime
    – ‘end_date’: as datetime

    3: add 2 ACF field to the post_type ‘news’
    – ‘target_date’: as datetime
    – ‘selected_campaign’ as relationship for post_type ‘campaign’

    4: make 10 campaign posts with ‘start_date’ & ‘end_date’ as publish.

    5: make new ‘news’ post

    6: disable ‘selected_campaign’ field till ‘target_date’ is selected.

    7: select ‘target_date’ as ‘2016/08/01’

    8: reload ‘selected_campaign’ result. the shown ‘campaign’ posts in the left pane will have to have ‘2016/08/01’ between their ‘start_date’ and ‘end_date’ (before submitting).

    6 is imaginable to make it by javascript,
    but 8 seems difficult for me so far.
    I think I need to fire a certain trigger for ajax post loading, but have no clue yet…

    I am gonna appreciate any kind of hint or advise.

    Thanks in advance!

  • This is something I’ve been working on and just finished. It gives an idea of what’s involved in creating fields loaded dynamically using AJAX based on a value of another field. This example uses 2 select fields and does not do everything that you need to do, but it does show how to load the values of one field through AJAX based on a change to another field.

  • Hi @hube2

    Big thanks for your clue!
    Codes works fine as they are.
    So I’m gonna start analysing them from now!

    One thing, I noticed that ajax_load_city_field_choices() affects oddly to field_type selection in field group edit screen. (red circled area in my attachment. Sorry for my screenshot in Japanese)
    Is it by design?

  • good catch. It appears that you should only enqueue the JS on the pages where you want it to run. I’ve updated this in the example.

    But as you can see, there’s quite a bit involved. I’ve done similar things with other fields in some projects I’ve worked on and I basically need to code every field individually. You might be able to do this with a post object field, but they are more difficult and I’ve never done one of those. This is because the post object field is a selec2 field and the value of the selected post is not as easy to get at or to update. When building things like this I tend to stick with the simpler fields.

  • Hi @hube2

    Thank you for your advice again!
    Somehow, I’ve done a hack for relationship field which I wanted to do finally.

    it was such a formidable one for me…
    Especially with relationship field, I had to care with both panes…

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

You must be logged in to reply to this topic.