Support

Account

Home Forums ACF PRO Pre-populate Relationship field with (default) value if not already set

Helping

Pre-populate Relationship field with (default) value if not already set

  • I would like to prepopulate a relationship field with a value. Specifically, only if the field is not already populated, and if there is a specific parameter in the URL.

    I did a print of the $field variable in load_value and I wasn’t able to find anything that would help. In other words, this doesn’t help, and i believe it’s because there is no “default value” option.

    
    add_filter('acf/load_field/key=field_58f0632672126', 'rel_field');
    function rel_field($field) {
    
    	$slider_id = isset($_GET['slider_id']) ? $_GET['slider_id'] : null;
    	if(isset($slider_id)) {
    	    $field['default_value'] = '';		
    	}
    	
    	return $field;
    }
    
  • Hi @jacobraccuia

    Thanks for the post.

    You are right, the field type does not contain a default_value argument within its settings array. As a workaround, you could load the new value to the ‘value’ array like so:

    add_filter('acf/load_field/key=field_58f0632672126', 'rel_field');
    function rel_field($field) {
    
    	$slider_id = isset($_GET['slider_id']) ? $_GET['slider_id'] : null;
    	if(isset($slider_id)) {
    	    $field['value'] = $value;	//pass an array with the new value(s)	
    	}
    	
    	return $field;
    }
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.