Support

Account

Home Forums General Issues Replacing the_content with custom fields

Solved

Replacing the_content with custom fields

  • What is the proper way to code a function which replaces the whole the_content with formatted ACF fields? This is needed to generate proper excerpts, RSS texts etc.

  • Why do you want to do that?

  • I use several smaller texts surrounded with formatted code – it’s like using forms in the admin. The original text input area remains empty, I changed the content-post.php, which looks good, but breaks some wp functionality, like excerpt generation, rss texts, etc. I guess the proper way should be using a function or filter to replace the_content or post_content.
    I made something like this – still not the right code.

    function new_filter_content($content) {
    	if( is_single() && is_main_query() ) {
      		if(get_field('what')) {$new_content .= 'blah' . get_field('what') . 'blah';} 
    		if(get_field('where')) {$new_content .= 'blah' . get_field('where') . 'blah';} 
    		 $content = $new_content;}	
    	return $content;}
    add_filter('the_content', 'new_filter_content');
    
  • Ah okay ..

    Well I think this should do it..just change some of the content for your own!

    
    function my_acf_save_post( $post_id ){
    
    	if(get_post_type($post_id) == 'your specific post type'){ //just for good measure so it doesn't run on unnecessery posttypes
    		// vars
    		$fields = false;
    	 
    		// load from post
    		if( isset($_POST['fields']) ){
    			$fields = $_POST['fields'];
    			$new_content = '';
    			
    			//add all of your content to $new_content
    			$new_content .= 'blah'. $fields['fieldname']['value'] . 'blah'; //not sure if Im getting the fieldvalues correctly
    			
    			// Update post
    			$my_post = array(
    			'ID'           => $post_id,
    			'post_content' => $new_content
    			);
    			// Update the post into the database
    			wp_update_post( $my_post );
    		}
    	}
    }
     
    // run after ACF saves the $_POST['fields'] data
    add_action('acf/save_post', 'my_acf_save_post', 20);
    
    

    EDIT: I think this might result in an infinite loop.. if so try this instead:

    
    function my_acf_save_post( $post_id ){
    
    	if(get_post_type($post_id) == 'your specific post type'){ //just for good measure so it doesn't run on unnecessery posttypes
    		// vars
    		$fields = false;
    	 
    		// load from post
    		if( isset($_POST['fields']) ){
    			$fields = $_POST['fields'];
    			$new_content = '';
    			
    			//add all of your content to $new_content
    			$new_content .= 'blah'. $fields['fieldname']['value'] . 'blah'; //not sure if Im getting the fieldvalues correctly
    			
    			// Update post
    			$my_post = array(
    			'ID'           => $post_id,
    			'post_content' => $new_content
    			);
    			// Update the post into the database
    			remove_action('acf/save_post', 'my_acf_save_post', 20);
    			wp_update_post( $my_post );
    			add_action('acf/save_post', 'my_acf_save_post', 20);
    		}
    	}
    }
     
    // run after ACF saves the $_POST['fields'] data
    add_action('acf/save_post', 'my_acf_save_post', 20);
    
    
  • Thanks, almost there, the blahs appeared in the_content. I am not sure what I have to replace in the code, when using my real ACF field names, they do not show.

    $new_content .= 'blah'. $fields['what'] . 'blah';

  • Well first you’ll want to replace

    
    if(get_post_type($post_id) == 'your specific post type'){ 
    

    “your specific post type” with the actual post type slug.

    Then you want to add in all of your fields in the right order to the new_content variable:

    
    
    $new_content .= 'this is the beginning of the content and heres my first field'. $fields['fieldname']['value'] . '. Doesnt it look marvelous!'; 
    $new_content .= ' Here's some more stuff added on to the content:'. $fields['fieldname']['value'] . ' and ' . $fields['fieldname']['value']; 
    
    

    The problem is that I don’t know what the $fields variable look like so in order for you to find out you can do this:

    
    print_r($fields);
    die();
    

    put it right after

     
    $fields = $_POST['fields']; 
    

    when you save a post you’ll get a blank screen with the values of $fields.

  • Edit: I had to take out the apostrophes… Everything works fine now.

    $new_content .= 'blah'. $fields[field_51bde56567f28] . 'blah';

    Many thanks!

  • You’re welcome 🙂

    Perhaps you could set the proper correct answer. I help people out to give back but it’s also nice to get acknowledged for it 🙂

  • Just one more thing, I noticed an odd behaviour when I updated the previous posts manually. Sometimes the content totally disappeared from the page (while still there in the admin). When trying to restore it from the revisions, it seemed like a few minutes after my manual update there was one more, totally blank save. Strange enough…

  • Most likely post revisions messing with the code. Add this right at the beginning of your function

    
    if (wp_is_post_revision( $post_id ) ){ return false; } 
    
  • Works just perfectly now. Thanks for the help.
    (btw – I don’t remember if I marked my own answer as the solution, I just used the checkbox above the submit. How can I undo this?)

  • No problem 🙂

    Honestly I have no idea how to undo it.. I’ve never made a question here myself =P

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

The topic ‘Replacing the_content with custom fields’ is closed to new replies.