Support

Account

Home Forums Backend Issues (wp-admin) Convert repeater to single field

Solved

Convert repeater to single field

  • Hi,

    One of my customer want to modify some ACF fields from a ancient website.
    In one case he want to convert, as it is not needed anymore, a repeater field to a single value field wich must be fill with the first value of the repeater.
    Any advice on how to do this without copy/paste hundreds times?

    Thanks 😉

  • Unfortunately, there really is not an easy way to do this, the only thing that I can do is to tell you what I would do given the same scenario.

    1) I would not delete the repeater

    2) I would create the new fields

    3) I would create a filter (https://www.advancedcustomfields.com/resources/acf-load_value/) that runs when the value is loaded for the new fields. If the new field does not have a value (has never been set) then retrieve the corresponding value from the old field. This will work to automatically transfer the old values to the new field when a post is edited.

    4) I would create and action (https://www.advancedcustomfields.com/resources/acf-save_post/). In this action I would delete the old fields from each post as it is updated using the new fields.

    5) With #3 & #4 in place and working successfully I would create another filter (https://www.advancedcustomfields.com/resources/acf-prepare_field/) for the repeater to hide the old field (return false). This leaves the old fields and data in place and sill retrievable for #3 and for #6 (coming up)

    6) Alter the template file(s) where the old values are used. First see if the new field has a value and if it does use the new field. If the new field does not have a value then to get the value from the old field and use it.

  • Thanks John, you are always so usefull !!

    Just for now I started with a simplified version :

    
    function update_fields_value( $post_id ) {
    	if (get_post_type($post_id) != 'annuaire') {
    		return;
    	}
    
    	$localisations = get_field('ACF_localisation', $post_id );
    	if ( $localisations ) {
    		$first_localisation = $localisations[0];
    		update_field( 'field_5f0846f3dbd27', $first_localisation['ACF_adresse_ligne_1'], $post_id );
    		update_field( 'field_5f08542b4627f', $first_localisation['ACF_adresse_ligne_2'], $post_id );
    		update_field( 'field_5f08544746280', $first_localisation['ACF_code_postale'], $post_id );
    		update_field( 'field_5f08548f46281', $first_localisation['ACF_localite'], $post_id );
    	}
    	$contacts = get_field('ACF_presentation_du_personnel', $post_id );
    	if ( $contacts ) {
    		$first_contact = $contacts[0];
    		if ( $first_contact['ACF_poste_du_contact'] != "" ) {
    			$contact_details = $first_contact['ACF_prenom_du_contact'] . ' ' . $first_contact['ACF_nom_du_contact'] . ' (' . $first_contact['ACF_poste_du_contact'] . ')';
    		} else {
    			$contact_details = $first_contact['ACF_prenom_du_contact'] . ' ' . $first_contact['ACF_nom_du_contact'];
    		}
    		update_field( 'field_5f0844ed062f3', $contact_details, $post_id );	
    	}
    }
    add_action('acf/save_post', 'update_fields_value', 15);
    

    I have just seen 5/6 posts for the above code testing and realise that there are encoding errors. With this solution I will force the customer to at least open, save (to update the fields) and check the new contents.

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

The topic ‘Convert repeater to single field’ is closed to new replies.