Home Forums Backend Issues (wp-admin) Get_field_objects or get_fields inside acf/load_field causes fatal error Reply To: Get_field_objects or get_fields inside acf/load_field causes fatal error

  • I fixed my issues and if anyone runs into a similar problem here is my solution.

    As using the above-mentioned functions (get_fields & get_field_objects) in any form caused an infinite loop I made a workaround for my problem. Solved it by writing a separate function which uses get_post_meta to fetch all fields using the post ID of the ACF field group. These fields are then quickly filtered to remove metadata. There is also a check for a field value using get_field, which is an optional step to hide fields without a value set. This could potentially cause a lot of queries if you have lots of fields.

    I load the function on admin_init, which is probably not the best hook to use, if someone has any pointers into using a better hook for this I would like to know.

    The fixed code is following.

    function fetch_custom_field_group( $acf_post_id ) {
    	$post_meta = get_post_meta($acf_post_id);
    	if ( ! is_array($post_meta) ) {
    	foreach ( $post_meta as $item ) {
    		// Unserialize get_post_meta data into a usable array
    		$item = unserialize($item[0]);
    		// Check whether field has a value set
    		$field = get_field( $item['name'] );
    		// Check if unserialize returned and whether the field has a key, filtering out any unnecessary fields
    		// optionally at last add a check to not show fields with empty values
    		if ( $item === false || empty($item['key']) || empty($field) ) {
    		// Fill everything into a new array that can be used
    		$acf_fields[] = $item;
    	return $acf_fields;
    add_action('admin_init', 'fetch_custom_field_group');

    The rewritten function for loading field choices is separate from the actual hook and can be reused, just by passing the ACF field group ID into the 2nd function argument.

    function acf_load_field_choices( $field, $acf_post_id ) {
    	// Empty the choices array, ensuring nothing is there
    	$field['choices'] = array();
    	// Get all fields from the ACF field group we are using
    	$choices = fetch_custom_field_group($acf_post_id);
    	// Get names to be used as keys for the input fields
    	$choice_name = array_column( $choices, 'name' );
    	// Get labels to be used as labels for the input fields
    	$choice_label = array_column( $choices, 'label' );
    	// Combine into key-value pair of names and labels
    	$choices = array_combine( $choice_name, $choice_label );
    	$field['choices'] = $choices;
    	return $field;

    And finally, for acf/_load_field filter, I just return the previous function passing the ID of the ACF field group I want to use.

    function acf_load_my_checkbox_field( $field ) {
    	return acf_load_field_choices($field, 198);
    add_filter( 'acf/load_field/name=your_field_name', 'acf_load_my_checkbox_field' );

    Screenshot of result: ACF dynamically generated checkbox field based on other ACF fields