Support

Account

Home Forums General Issues Migrate content from ACFs to the_content

Solving

Migrate content from ACFs to the_content

    • Erikr1

    • July 14, 2020 at 9:39 pm

    Hello There,

    Could anyone help me, please?

    I have 5 ACF in every post, which split content in 5 chapters (chapter_1, chapter_2 and so on). Practically I’d like to take the content from the fields and move it into the post section keeping formatting and the rest. A solution would be to do it by hand but 400 posts would take quite a while. Is there a way to automate this? There is a similar topic in the forum with this code but I can’t seem to make it work. It only change the latest post before spitting an error.

    <?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();
    
    ?>
  • all of the references to $my_post in your loop should be $post

    • Erikr1

    • July 28, 2020 at 5:56 pm

    Ok, I swapped $my_post for $post as advised and now the content is copied over but in 2-3 instances. It’s like if the loop happens multiple times before settling.

    The code looks like this now:

    <?php
    global $post;
    $args = array( 
    	'post_type' => 'post', 
    	'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('chapter_1');
    	$custom_field_text_only = wp_strip_all_tags($custom_field_raw);
    
    	$post = array(
    		'ID'           => get_the_ID(),
    		'post_content' => $custom_field_raw
    	);
    
    	wp_update_post( $post );
    
    endforeach; 
    wp_reset_postdata();

    Ideally I’d need to move all chapters at once and make sure the loop doesn’t create multiple instances of every field. Could you help, please?

    THANK YOU!

    EDIT:
    I’m using this to add PHP to the page: https://wordpress.org/plugins/insert-php-code-snippet/

  • I don’t see anything in the code that would cause it to create multiple instances of a posts. It may have something to do with the plugin but don’t know why that would cause it either.

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.