Home Forums Backend Issues (wp-admin) Lookups: how to use a Select to update other fields?


Lookups: how to use a Select to update other fields?

  • Hello,

    Is it possible when using Select field as a lookup to update the value of other fields on the same form?

    For example: if there was a Select field that contained a list of ‘venue names’, once a selection was made the various ‘address’ fields below it would be populated with the values corresponding to that venue.

    So far, I can see how the ‘venue names’ Select could be dynamically populated with various values. But, I’m certain how the selection of a ‘venue name’ could trigger and also update the values of the related fields on the post being edited.

    Any help would be greatly appreciated!

  • HI @paulswanson

    I guess it depends on how the data is stored initially?

    Is Venue a custom post type with various ACF fields (name, address etc.)

    If so, you may be able to use the venue name in the select option field but use the post ID as the option value, then use AJAX to retrieve the rest of the data and populate the other fields in the form.

  • As mentioned above, it depends on how you store the venue data.

    If you can dynamically populate the select, set the value as the venue ID
    Then set the option label as the address

    You could then use the acf/save_post hook for when the form is triggered, something like:

    function my_acf_save_post( $post_id ) {
        // Get newly saved values.
        $values = get_fields( $post_id );
        // Check the new value of a specific field.
        $select_field = get_field_object('select_field', $post_id);
    	//we now have the post ID of the venue
    	$venue_id = $select_field['value'];
        if( $venue_id ) {
    		$address_line_1 = get_post_meta( $venue_id , 'address_line_1', true );
    		update_post_meta( $post_id, 'address_line_1', $address_line_1 );
    		#repeat for other address fields
    #acf/save_post action after ACF has saved the $_POST data. This is possible by using a priority greater  than 1.
    add_action('acf/save_post', 'my_acf_save_post', 20);

    So when then form is triggered, you get can then get the venue ID

    Once you have this, you can get the various address elements and update the post populating the various address fields.

    Just a thought! Trying to go the Ajax route may be a headache

  • Some time ago I created an example of using AJAX to alter the options in one select field based on a selection made in another select field. As far as I know it still works and can be used as an example of that needs to be done.

  • That’s awesome @hube2, I think I may bookmark that one if you don’t mind!

  • @jarvis that’s why I created them. But that’s one of the few examples that still works. The JS is ACF changed at version 5.7 and this is the one of two AJAX examples that I updated for this change because I use things similar to this all the time.

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

You must be logged in to reply to this topic.