Support

Account

Home Forums General Issues writing a script to rename the custom fields

Solving

writing a script to rename the custom fields

  • Hello, so this is my issue
    my custom fields are named in Russian, and it’s a big mistake, and my db is big already
    Now i want to rename the fields, and i find out that changing the name from the plugin dashboard is not enough

    so i found a script that duplicate the same fields to and create new meta + deleting the old meta

    my question: is there any consequences doing that?
    another question: i found meta keys that start with dash (_) and they point for some field, what is that? is it okay to duplicate them also to the new name?

    This is the script

    
    
        $meta_keys = array(
    
            array( 'old_field','new_field' ),
    
        );
    
        foreach ( $meta_keys as $k ) {
    
            $args = array(
                'post_type'      => 'event',
                'posts_per_page' => -1,
                'post_status'    => 'publish',
                'meta_key' => $k[0]
            );
    
            $the_query = new WP_Query( $args );
    
            if ( $the_query->have_posts() ) {
    
                while ( $the_query->have_posts() ) {
                    $the_query->the_post();
                    $meta = get_field( $k[0]);
                    if ( $meta ) {
                        // Migrate the meta to the new name
                        update_field($k[1], $meta );  // add the meta with the new name
                        delete_post_meta(get_the_ID(),$k[0]);                    // delete the old meta
    
                    }
                }
            }
    
            wp_reset_postdata();    // Restore original Post Data
    
        }
    
  • Each field has 2 entries in the database.

    “field_name” = the field value
    “_field_name” = field key of field_name

    ACF manages this when you use update_field()

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

You must be logged in to reply to this topic.