Support

Account

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

Solved

acf/update_value & wp_update_post to copy WYSIWYG to the_content() in template?

  • I’ve never done this, so I’m really just kind of looking for a sanity check.

    The situation is I have a custom post template that disables the native WordPress text box, leaving only the ACF fields, including a few WYSIWYG fields and tons of others.

    The problem is that I’m grabbing the first X words out of the_content(), which won’t be used by the client on this template, in order to populate the_excerpt() on the homepage / archive loops. (You know, how WordPress usually does it if you don’t define an explicit the_excerpt(), it yanks it from the_content()).

    So, that means I need to copy the first WYSIWYG ACF field into the_content(). I understand this will be done with:

    • acf/update_value/name=my_field_name
    • wp_update_post

    I’ve seen some examples here in the forum for writing a function to do this.

    My first question is: Can I put this inside my single-custom-post-template.php in order to only run the function on these specific posts?

    My second question is can someone look at my function and help me understand some stuff (and also spot any errors if they’re there)?

    
    function my_acf_copy_content( $value, $post_id, $field ) {
        wp_update_post( array( 'ID' => $post_id, 'post_content' => $value ) );
        return $value;
    }
    add_filter('acf/update_value/name=my_field_name', 'my_acf_copy_content', 10, 3);
    

    Does that look correct?

    Is the function receiving the $field variable from the acf/update_value/name= filter or do I need to pass it in somehow?

    Also, I don’t understand why people are suggesting we return $value.

    Any help and insight here is appreciated, thanks a ton.

  • Okay, I’ve answered my first question. This seems like a no-go inside the page template (though I seem to have an error).

    I think it’s going to have to go into functions.php and it SEEMS like I can wrap it in an if statement for is_page_template(‘single_custom_post_template.php’)

    But I’ve done that and it’s not working.

  • Sorry to bump this thread like this.

    So my attempt above in the functions.php, while wrapped in the is_page_template() didn’t copy to the_content(); It actually just wiped that WYSIWYG box entirely, deleting the content from it.

    I removed the IF is_page_template(); and it worked though…

  • 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.

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.