Support

Account

Home Forums Backend Issues (wp-admin) Populate field choices with wp_insert_post

Solved

Populate field choices with wp_insert_post

  • Hi,

    I have a function which is called when I insert a new post.
    Inside this function I have an array with some data. I have also registered a acf select field.

    I am trying to update the acf field’s choices with the values from my array. It seems to update the field but it doesn’t shown up on the field in the admin area.

    My code is this:

    function xml_data(){
    $data_array; //my array with my data
    $field = get_field_object('field_1');
    $field['choices'] = array();
    foreach($$data_array as $choice){
    	$field['choices'][ $choice ] = $choice;
      }
    }
    
    add_action('wp_insert_post', 'xml_data');

    and also:

    if( function_exists('acf_add_local_field_group') ):
    
    acf_add_local_field_group(array(
    	'key' => 'group_1',
    	'title' => 'My Group',
    	'fields' => array (
    		array (
    			'key' => 'field_1',
    			'label' => 'Sub Title',
    			'name' => 'sub_title',
    			'type' => 'select',
    			'choices' => array(
    
    			)
    		)
    	),
    	'location' => array (
    		array (
    			array (
    				'param' => 'post_type',
    				'operator' => '==',
    				'value' => 'post',
    			),
    		),
    	),
    ));
    
    endif;

    All of the aboves are in my functions.php file.
    Can anyone help please? Thank you

  • In order to do this you need to call get_field_object() and you should do this using the field key, not the field name. Alter the choices for the field as you need to and then call acf_update_field($field). This is an undocumented function and is the function ACF calls when you change a field in the admin.

  • As you can see, I am already use the get_field_object() with the key field (and if I print I get the right object with the right choices). Should I just added the
    acf_update_field($field) after my for loop?

    Thank you

  • Sorry, I did not see the use of the get_field_object() function.

    Yes, after your loop you need to use the other function to actually make the changes to choices permanent in the database.

  • Now that I look at this…. what I said will not work as this is not a field that is stored in the database. You cannot make this change in the way you are attempting to do it.

    For something like this you need to make this change every time the field is loaded using an acf/prepare_field filter.

  • Ok, I see.
    So should I change my code to this:

    function xml_data(){
    $data_array; //my array with my data
    $field = get_field_object('field_1');
    $field['choices'] = array();
    foreach($$data_array as $choice){
    	$field['choices'][ $choice ] = $choice;
      }
    
    return $field;
    }
    
    add_action('wp_insert_post', 'xml_data');
    apply_filters( 'acf/prepare_field', 'xml_data' );

    Could it work that way?
    Thank you

  • I do not know because I don’t know how the data is being supplied.

    Because your field is defined in PHP you must change the choices in PHP on every use of the field. What this means for you depends on what you need to do with the field choices and when you need them available. You may need to use acf/prepare_field or possibly acf/load_field or maybe even both.

    The data that you want to use will need to be stored in some manor so that it can be loaded and used for the choices each time the field is used.

    Where are you getting this data from that you’re using for the choices?

  • I am getting the data from an xml file. When I start a new post,
    I run the function, so I can read the xml and process the data I need and finally push them in an array.
    And I want to display this array as the field options.
    If you think there is another way, I will be happy to try it.

  • My suggestion is that you only populate the choices for the field on the acf/load_field hook https://www.advancedcustomfields.com/resources/acf-load_field/ with a priority of 20 and I would probably use the field key variant of the hook.

  • I used this add_action( 'acf/prepare_field/key=field_1', 'xml_data' ); instead of wp_inser_post and it seems to work fine. And it also seems to work with both load and prepare field. Thank you again!

  • So that you know, prepare field will only work to populate choices in the admin when editing a post. When using get_field() or the_field() the field will return values selected but it will not return the correct label if the label is different than the value. Also, if you use get_field_object() the choices will not be populated in the returned field object.

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

You must be logged in to reply to this topic.