Support

Account

Home Forums General Issues Replacing the_content with custom fields Reply To: Replacing the_content with custom fields

  • 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);