What I had was an ACF form on a frontend page where the user could create new posts. Something like this:
acf_form( array(
'form' => true,
'id' => 'prefix-acf-form',
'post_id' => 'new_post',
'new_post' => array(
'post_type' => 'post_type_here',
'post_status' => 'publish',
),
'field_groups' => array( Class_Here::get_acf_key( 'edit' ) ),
'honeypot' => false,
) );
The idea being that I would save post_content
field as the actual post content for the post.
The problem was that I was using global $post
, so the pages content was being displayed instead of the expected nothing.
The solution was to check if the passed $post_id
is either empty or new_post
and if it is, return the value early before attempting to get the post content.
/**
* Return post content if possible
*
* @param String $value - Field content
* @param Mixed $post_id - Post ID, Empty, or 'new_post'
*
* @return String - Post content for given post ID
*/
function themeprefix_acf_post_content( $value, $post_id ) {
// We already have a value for some reason, return it.
if( ! empty( $value ) ) {
return $value;
}
// We do not have a $post_id to pull from, return early.
if( empty( $post_id ) || 'new_post' == $post_id ) {
return $value;
}
return get_post_field( 'post_content', $post_id, 'edit' );
}
add_filter( 'acf/load_value/name=post_content', 'themeprefix_acf_post_content', 10, 2 );