Support

Account

Home Forums General Issues Copy custom field to WordPress Editor field

Solved

Copy custom field to WordPress Editor field

  • Hi,

    When I first started/learning, I created a custom post with a custom excerpt and a custom editor. After a few hundred, I added support for excerpt and edit. Now I have a few hundred originals that have excerpt and thousands opposite.

    To fix this, I want to copy the custom expcerpt field and the custom editor field to the native WP excerpt and editor fields.

    1.) query all posts
    2.) while have posts,
    3.) loop through and write one to the other.

    
    <?php 
    	foreach ($custom_posts as $custom_post) { 
    
    		$legacy_content = get_field( 'legacy_content' );
    		$legacy_excerpt = get_field( 'legacy_excerpt' );
    		$excerpt = get_the_excerpt();
    		$content = get_the_content();
    
    		if ( !$legacy_content == '' && $content == '' ) {
    			// Write legacy_content to wp native editor content?
    			$legacy_content = '';
    		}
    
    		if ( !$legacy_excerpt == '' && $excerpt == '' ) {
    			// Write legacy_excerpt to wp native excerpt content?
    			$legacy_excerpt = '';
    		}
    
    		// UPDATE post
    	}
    ?>
    

    How do I write one to the other, clear out the legacy and UPDATE post?

    Thanks!

  • Thanks. That got me on the right track. If this can help anyone, here is how I did it. After I used phpmhyadmin to remove the fields I didn’t need. I didn’t concern myself with the excerpt because wp auto generates it.

    
    		$args = array( 'post_type' => 'custom_post_name', 'posts_per_page' => -1 );
    		$loop = new \WP_Query( $args );
    		$count = 1;
    		while ( $loop->have_posts() ) : $loop->the_post();
    
    			$legacy_content = get_field( 'legacy_content' );
    			$content = get_the_content();
    
    			// If custom content field is not empty and wordpress native content is empty
    			if ( !$legacy_content == '' && $content == '' ) {
    				$post_args = array(
    					'ID'           => get_the_ID(),
    					'post_content' => $legacy_content, // update legacy content field to post_content
    					
    				);
    				wp_update_post( $post_args );
    update_field( 'legacy_content', '', get_the_ID() );
    update_field( 'legacy_excerpt', '', get_the_ID() );
    			}
    
    		endwhile;
    
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.