Support

Account

Home Forums General Issues acf/update_value & wp_update_post to copy WYSIWYG to the_content() in template? Reply To: acf/update_value & wp_update_post to copy WYSIWYG to the_content() in template?

  • Alright, I’m documenting this in case anyone in the future needs it. Took extra care to put the right search terms in the thread title, etc.

    Here’s what I got working. Don’t use acf/update_value. You want to use ‘acf/save_post’.

    Next, in functions.php, for some reason using `if( if_page_template(”) )’ was not working at all. It was causing the field to be deleted.

    But you had to “qualify” it in some way to make sure you’re only running on the right custom post template. So what I did was a check to see if the field I’m copying from exists, and it ONLY exists on that custom post template. So that check works out.

    Here’s the code:

    // Take the first WYSIWYG ACF field and copy it into the_content() in order to populate the_excerpt()
    function my_acf_copy_content( $post_id ) {
      if ( get_field('acf_site_text_box_top') ) {
        $value = get_field('acf_site_text_box_top');
        wp_update_post( array( 'ID' => $post_id, 'post_content' => $value ) );
        return $value;
      }
    }
    add_filter('acf/save_post', 'my_acf_copy_content', 20);

    Be careful because without the qualifier, it flat out deletes your entire post on any post not using that custom field, which you then have to retrieve from the revisions.