1

I try to replace post's the_content with value from ACF field article_text. I managed to achieve it with following method:

  $postid = get_queried_object_id();

  $my_post = array(
      'ID'           => $postid,
      'post_content' => get_field('article_text'),
  );

  wp_update_post( $my_post );

  the_content();

But I have a small issue with it: when I create post, the_content doesn't automatically gets the value from ACF article_text. I have to refresh the browser couple times in order to see that change. When I preview the post without publishing, I cannot see the_content at all.

My question - is there a more efficient way to do it to see the content straight away?

The reason why I want to display the_content instead of article_text is because of several not-ACF-friendly plugins.

1 Answer 1

4

Instead of updating the post's content, you can filter it using the the_content filter.

add_filter( 'the_content', 'wpse241388_use_acf_field' );
function wpse241388_use_acf_field( $content ) {
    return get_field( 'article_text' );
}

Update

To apply to only your article post type:

add_filter( 'the_content', 'wpse241388_use_acf_field' );
function wpse241388_use_acf_field( $content ) {
    if ( is_singular( 'article' ) ) {
        $content = get_field( 'article_text' );
    }
    return $content;
}

References

3
  • Actually I have one more question. If I want to apply it only to specific post type article. How would you recommend to modify your code?
    – ERDFX
    Oct 3, 2016 at 16:50
  • I've updated my answer. Use one or the other, not both, of the snippets.
    – Pat J
    Oct 3, 2016 at 16:54
  • Doing it this way doesn't allow for the use of <!--next page-->. Any way around this? Mar 30, 2017 at 7:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.