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);
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.