Support

Account

Home Forums ACF PRO ACF to the_content()

Solving

ACF to the_content()

  • I have tried everything I have found in the forum in various solutions and I’m not being able to get this to work.

    I am trying to have all the fields from a custom post type to get formatted with some html, and then save into the database cell for the posts content.

    So for example:

    ACF field names: “Bio”, “URL”, “Photo”, “Important Information”

    Gets some html added to them: <a href="URL">Bio</a><img src="Photo" style="color: #444444" /> <p>Important Information</p>

    Then saved into the_content.

    Then, whenever our RSS, Apple News, Email Blasts, or any others that automatically query the_content, they get all of the fields they need. We use a plugin from our corporate offices that we are unable to change code in so we cant change what they are querying. The others I could add code to make them look for the fields, but because I cant change that one plug in we need to be able to make sure everything they need is in the_content. Plus, I would rather not modify plugin code in case they are updated.

    This is the code I have been playing with trying to get it to work, but it isn’t.

    // Functions for rendering ASF CTP's into the_content() so that they will be served up to Apple News, RSS, BMCHS Connect App, and other feeds. 
    
    function my_acf_save_post( $post_id ){
    
    	if(get_post_type($post_id) == 'Irish Eyes'){
    
    		$fields = false;
    	 
    
    		if( isset($_POST['acf']) ){
    			$fields = $_POST['acf'];
    			$new_content = '';
    			$new_content .= $fields['fieldname']['value'];			
    
    			$my_post = array(
    			'ID'           => $post_id,
    			'post_content' => $new_content,
    			'post_type' => 'irish-eyes'
    			);
    
    			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);
    		}
    	}
    }
    add_action('acf/save_post', 'my_acf_save_post', 20);
  • I was able to get it to work by changing the calls to act/save_post to save_post. But its showing the content as Array. Not sure where to go from here now….

    function my_acf_save_post( $post_id ){
    
    		if( isset($_POST['acf']) ){
    			$fields = $_POST['acf'];
    			$new_content = 'before the fields';
    			$new_content .= $fields;			
    			$new_content .= 'after the fields';
    			
    			$my_post = array(
    			'ID'           => $post_id,
    			'post_content' => $new_content
    			);
    
    			remove_action('save_post', 'my_acf_save_post', 20);
    			wp_update_post( $my_post );
    			add_action('save_post', 'my_acf_save_post', 20);
    		}
    	
    }
     
    // run after ACF saves the $_POST['acf'] data
    add_action('save_post', 'my_acf_save_post', 20);
    
  • Hi @justintylermoore

    Keep in mind that the get_post_type() function will return the slug of your custom post type, not the name.

    Also, with the priority of “20” you can use get_fields() function instead of the $_POST variable. This page should give you more idea about it: https://www.advancedcustomfields.com/resources/acfsave_post/.

    I hope this helps.

  • Thanks James!

    What I posted in my code above is after trying just about everything. I tried placing the the slug (irish-eyes) in the get_post_type() function and it didn’t work either.

    As for the $_POST[‘acf’] variable, I tried get_fields() as well. In my followup comment from above, I was able to finally get it to save to the post content, but this is what it saves:

    before the fieldsArrayafter the fields

    How to I get that “Array” text to save all the content of the fields from the post and not the word Array?

  • Thanks @acf-support !

    What I posted in my code above is after trying just about everything. I tried placing the the slug (irish-eyes) in the get_post_type() function and it didn’t work either.

    As for the $_POST[‘acf’] variable, I tried get_fields() as well. In my followup comment from above, I was able to finally get it to save to the post content, but this is what it saves:

    before the fieldsArrayafter the fields

    How to I get that “Array” text to save all the content of the fields from the post and not the word Array?

  • Hi @justintylermoore

    $_POST[‘acf’] is an array, so if you join it with a string, it will show up as “Array”. You need to access the data inside the Array. An easy way to check the data is by using the var_dump() function. This page should give you more idea about it: https://www.advancedcustomfields.com/resources/debug/.

    To learn more about the Array type, please take a look at this page: http://php.net/manual/en/language.types.array.php.

    I hope this helps.

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

The topic ‘ACF to the_content()’ is closed to new replies.