Support

Account

Home Forums Backend Issues (wp-admin) Fields with the same key, created with PHP, are getting overwritten.

Solved

Fields with the same key, created with PHP, are getting overwritten.

  • Fields with the same key, created with PHP (register_field_group), are getting all properties inherited/overwritten from the last one.

    Let me explain, using an example: I create the fields in PHP and usually have all gallery fields with the key “field_photos” so it’s easier for me to code the wordpress templates. So I have “field_photos” on an “Artist” post type, and I also have another field with the same key on “Exhibition” post type.

    The thing is that sometimes these fields have slightly different properties, like a different label, like “Artwork” for the artists and “Photos” for the exhibitions. And the thing is that it I create the field for the artist first, then the exhibitions, the label shows “Photos” for both cases!

    I looked everywhere in my code for any fault I could’ve done, and I narrowed it down to the field key. I believe that ACF loads the field data looking for the field key. And what happens it finds the last field created, even thought they were created in different “register_field_group” calls, and even for different location, and even having the field group ID set differently.

    And that biggest problem is that is not only the label that changes, sometimes it’s a bunch of other properties! All the field options are set as the last one.

    Could you please help me up?

    It would be nice for me to have the field key the same, it eases a lot on developing a common template for all pages.

    Here is a code example:

    
    $field_args = array(
    	'id' => 'acf_'.ARTISTAS,
    	'title' => 'Editar',
    	'menu_order' => 1,
    	'options' => array(
    		'position' => 'normal',
    		'layout' => 'no_box',
    		'hide_on_screen' => array(
    		),
    	),
    	'location' => array(
    		array(
    			array(
    				'param' => 'post_type',
    				'operator' => '==',
    				'value' => ARTISTAS,
    				'order_no' => 0,
    				'group_no' => 0,
    			),
    		),
    	),
    	'fields' => array(
    		array(
    			'key' => 'field_mytext',
    			'name' => 'text',
    			'label' => 'Text Field',
    			'type' => 'text',
    			'default_value' => '',
    		)
    	)
    );
    
    register_field_group($field_args);
    
    $field_args = array(
    	'id' => 'acf_'.EXPOSICOES,
    	'title' => 'Editar',
    	'menu_order' => 1,
    	'options' => array(
    		'position' => 'normal',
    		'layout' => 'no_box',
    		'hide_on_screen' => array(
    		),
    	),
    	'location' => array(
    		array(
    			array(
    				'param' => 'post_type',
    				'operator' => '==',
    				'value' => EXPOSICOES,
    				'order_no' => 0,
    				'group_no' => 0,
    			),
    		),
    	),
    	'fields' => array(
    		array(
    			'key' => 'field_mytext',
    			'name' => 'text',
    			'label' => 'Another label',
    			'type' => 'text',
    			'default_value' => '',
    		)
    	)
    );
    
    register_field_group($field_args);
    
    //both fields are labeled as "Another label"
    
  • Hi @andrefelipe

    The point of the field’s key is to be a unique identifier. The field name may be the same, but never the field key.

    There is no solution or fix to this other than modifying your fields to use different keys.

    You can use the field_name in your template files (get_field($field_name)).

    Thanks
    E

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

The topic ‘Fields with the same key, created with PHP, are getting overwritten.’ is closed to new replies.