Web Development & WordPress

ACF post object drop down value auto populate or selected based on other field value

In the screenshot you can see the Post Object in ACF and Field Name is “region_id”.

That drop down ( Region) I have to auto select or populate based on other field ( Country) .

You need to put the code either in function.php or using custom snippet plugins.

  add_filter('acf/load_field/name=region_id', function($field) {
    // Check if we are on a valid post edit screen
     
        $post_id = $_GET['post'];

        // Get the value of the 'p_country' field for the current post
        $p_country_value = get_field('p_country', $post_id);

     
        if ($p_country_value === 'New Zealand') {
            $field['default_value'] = array(46627);
        }
	 if ($p_country_value === 'United States') {
         $field['default_value'] = array(46628);
        }
	  if ($p_country_value === 'Australia') {
         $field['default_value'] = array(46629);
        }

       
        $field['instructions'] = $p_country_value;
         
   

    return $field;
}, PHP_INT_MAX);

Leave a comment