Support

Account

Home Forums Backend Issues (wp-admin) acf/load_field not displaying values

Solved

acf/load_field not displaying values

  • I’m using acf/load_field to load subfields of a group. It seems to be working fine to load all of the fields, but when I try to make a choice or change any value, nothing happens.

    It’s actually saving my values to the database but not in the admin interface. I’m thinking that all of the fields are just getting reloaded on every page refresh with their default values for some reason. What do I do?

  • Can you supply code?

    No idea what “…using acf/load_field to load subfields of a group…” means

  • Sorry, it’s a lot of code, but thank you for your time. I don’t know the best way to share it, but hopefully this is okay.

    I’m using this filter:

    add_filter('acf/load_field/key=field_604de6ee3ffc6', 'acf_load_post_type_groups');

    to add subfields to a field of type “Group” (because I need them to be dynamic).

    Here is the group field:

    acf_add_local_field_group(array(
    	'key' => 'group_604dda92ef574',
    	'title' => 'Archives',
    	'fields' => array(
    		array(
    			'key' => 'field_604de6ee3ffc6',
    			'label' => 'Archives',
    			'name' => 'archive_options',
    			'aria-label' => '',
    			'type' => 'group',
    			'instructions' => 'Archive pages are lists such as the main blog page, search results, and category and tag pages. Choose layout, appearance, and other options for such pages from the list below for each post type.',
    			'required' => 0,
    			'conditional_logic' => 0,
    			'wrapper' => array(
    				'width' => '',
    				'class' => '',
    				'id' => '',
    			),
    			'layout' => 'block',
    			'acfe_seamless_style' => 0,
    			'acfe_group_modal' => 0,
    			'acfe_group_modal_close' => 0,
    			'acfe_group_modal_button' => '',
    			'acfe_group_modal_size' => 'large',
    			'sub_fields' => '',
    		),
    	),
    	'location' => array(
    		array(
    			array(
    				'param' => 'options_page',
    				'operator' => '==',
    				'value' => 'theme-options',
    			),
    		),
    	),
    	'menu_order' => 10,
    	'position' => 'normal',
    	'style' => 'seamless',
    	'label_placement' => 'top',
    	'instruction_placement' => 'label',
    	'hide_on_screen' => '',
    	'active' => true,
    	'description' => '',
    	'show_in_rest' => 0,
    	'acfe_display_title' => '',
    	'acfe_autosync' => '',
    	'acfe_form' => 0,
    	'acfe_meta' => array(
    		'604dddb8749bf' => array(
    			'acfe_meta_key' => '',
    			'acfe_meta_value' => '',
    		),
    	),
    	'acfe_note' => '',
    ));

    The ‘sub_fields’ => ”, part is getting loaded from a function in functions.php using that filter.

    Here’s the function:

    (ATTACHMENT-1)

    Here’s what I get when I look at the php that is being generated by ACF:

    (ATTACHMENT-2)

    And here’s how it looks on my Theme Options page:

    archive-options-page

    Each post type has the same set of options. Everything else on the options page is saving just fine, but not this stuff.

    My fields are getting generated but nothing is saving. I originally said that it was saving to the database, but it’s not now.

    The database looks kind of weird, too:

    acf-database-rows

    Any idea what I’m doing wrong? I assume there’s something seriously wrong with that function even though the fields are getting generated.

  • You cannot use acf/load_field to create sub fields in an existing field in a field group, even if those fields are defined in another group.

    To add fields to a group you would have to use the acf/load_field_group filter. This is an undocumented filter.

    To be completely honest do not understand what you’re trying to accomplish here. Fields need to be defined when the field group is defined not when the field is loaded.

    Fields can be defined and added to field groups and even as sub fields using the acf_add_local_field() function and defining the parent of the field.

    https://www.advancedcustomfields.com/resources/register-fields-via-php/

    Other things noticed in your code. All field keys must begin with “field_”, “sub_field_” will not work.

  • Thanks! That was very helpful. What I was trying to do was loop through a certain set of post types and dynamically add them as subfields of a group along with a bunch of subfields for each post type.

    You were correct that I should be doing it when the group field is registered, not when it loads.

    Also, all field keys should start with ‘field_’ not ‘sub_field_’. I still could not get the options to save, however, because of another problem.

    Because I was using a foreach loop to create the subfields on each post type, I was using a random number on each pass through the loop, trying to prevent duplicate field keys.

    What I didn’t realize was that this meant the field keys kept changing. Every time I tried to save options, the field key would change, so of course nothing was saving in the database!

    So, to fix it I just had to use the post type in the field keys instead of a random number. That way they would be unique AND permanent. Problem solved!

    Thanks again.

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

You must be logged in to reply to this topic.