Support

Account

Home Forums General Issues Migrate a custom field content to the_content

Solved

Migrate a custom field content to the_content

  • Is there any way to move automatically the content of a custom field to the_content(); wp field?
    I need to make this change in a website with more than 400 articles, and is too hard to make it manually. Is there any easy way?
    I want to do it to be able to use plugins for related posts, SEO and search, and all loads info from the_content(); . The field actually used for the main content is called “desarrollo_articulo” and the_content(); is empty.

    Thanks a lot!!!

  • Hi @animalejourbano

    To bulk update this value, I would create a new page template where you can write and run some custom code.

    This template would use the WP_Query / get_posts function to query the DB for all the posts you want to update.

    Then, loop through all the posts and for each post, load the ACF value (get_field()), and then use the wp_update_post function to set the post_content with the ACF value.

    Hope that helps.

    Thanks
    E

  • Blast from the past — @animalejourbano + anyone who needs it:

    As @elliot mentioned, pop something like this somewhere (page template) where you can load it on the front end.

    Required: Replace YOURCUSTOMPOSTTYPE and YOURCUSTOMFIELD

    Optionally use $custom_field_raw if you’d like to maintain any formatting in your custom field.

    <?php
    
    global $post;
    $args = array( 
    	'post_type' => 'YOURCUSTOMPOSTTYPE', 
    	'posts_per_page' => -1,
    	'post_status' => 'any'
    );
    
    $myposts = get_posts( $args );
    foreach ( $myposts as $post ) : setup_postdata( $post );
    
    	$custom_field_raw = get_field('YOURCUSTOMFIELD');
    	$custom_field_text_only = wp_strip_all_tags($custom_field_raw);
    
    	$my_post = array(
    		'ID'           => get_the_ID(),
    		'post_content' => $custom_field_text_only // use $custom_field_raw if you want to keep any HTML formatting
    	);
    
    	wp_update_post( $my_post );
    
    endforeach; 
    wp_reset_postdata();
    
    ?>

    Hope this helps someone, someday 🙂


    mp

  • It did indeed help!
    I’m wondering if anybody would have a hint about changing this code to append the the custom field to the post_content instead of just replacing it.

  • @decioy This should work…

    <?php
    
    global $post;
    $args = array( 
    	'post_type' => 'YOURCUSTOMPOSTTYPE', 
    	'posts_per_page' => -1,
    	'post_status' => 'any'
    );
    
    $myposts = get_posts( $args );
    foreach ( $myposts as $post ) : setup_postdata( $post );
    
    	$custom_field_raw = get_the_content().get_field('YOURCUSTOMFIELD');
    	$custom_field_text_only = wp_strip_all_tags($custom_field_raw);
    
    	$my_post = array(
    		'ID'           => get_the_ID(),
    		'post_content' => $custom_field_raw // use $custom_field_raw if you want to keep any HTML formatting
    	);
    
    	wp_update_post( $my_post );
    
    endforeach; 
    wp_reset_postdata();
    
    ?>

    PS: I recommend using $custom_field_raw instead of text_only if you want everything to look like it should.

  • Hello There,

    Apologies for resuming this post.

    I’m trying to do exactly this but the code above changes only the latest post before spitting an error message.

    I have 5 ACF, which splits long posts in 5 chapters (chapter_1, chapter_2 and so on).
    I’d like to move away from it and move the content of all posts from the ACFs to the_content and given I’m an absolute nob I can’t seem to be able to do it.

    Could anyone help me through the process, please?

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

The topic ‘Migrate a custom field content to the_content’ is closed to new replies.