Home › Forums › Front-end Issues › acf_form NOT creating new Post
I seem to have the correct code based on the documentation, but a new post is not being created.
This is what I have in the functions.php file:
function my_pre_save_post( $post_id ) {
if ( $post_id != 'new' ) {
return $post_id;
}
$post = array(
'post_status' => 'publish',
'post_type' => 'post',
'post_title' => $_POST['fields']['field_54e6020d07a17'],
'post_content' => $_POST['fields']['field_54db8cd72d3c2']
);
$post_id = wp_insert_post($post);
$_POST['return'] = add_query_arg( array('post_id' => $post_id), $_POST['return'] );
return $post_id;
}
add_filter('acf/pre_save_post' , 'my_pre_save_post' );
And this is what I have on the template page:
acf_form( array(
'post_id' => 'new',
'fields' => array( 'field_54e6020d07a17', 'field_54db8cd72d3c2' ),
'updated_message' => 'Post Created'
));
I do have the acf_form_head() on the top of the template page and the acf_form is within the Loop.
How can I get this to create a new post?
I resolved my problem by changing the fields name from ['fields']
to ['acf']
The problem I am having now is editing a post. Here is the code in the functions.php file:
add_filter('acf/pre_save_post' , 'my_pre_save_post' );
function my_pre_save_post( $post_id ) {
if( $post_id != 'new_post' ) {
return $post_id;
}
$post = array(
'post_status' => 'publish',
'post_title' => $_POST['acf']['field_54e6020d07a17'],
'post_content' => $_POST['acf']['field_54db8cd72d3c2'],
'post_type' => 'post' ,
);
$post_id = wp_insert_post( $post );
$_POST['return'] = add_query_arg( array('post_id' => $post_id), $_POST['return'] );
return $post_id;
}
Here is the acf_form on the Add Post page template:
acf_form( array(
'post_id' => 'new_post',
'fields' => array( 'field_54e6020d07a17', 'field_54db8cd72d3c2' ),
'updated_message' => 'Post Created'
));
Here is the acf_form on the Edit Post page template:
acf_form( array(
'post_id' => $_GET['post_id'],
'fields' => array( 'field_54e6020d07a17', 'field_54db8cd72d3c2' ),
'submit_value' => 'Update the post!'
));
Can anyone help me figure this one out? Thanks
I’ve decided to come at this from a different angle and found a solution. All I need to save is the Post Title and Post Content which is really simple to do with v5 acf_form
. But I want to use a “Basic” wysiwyg editor and remove the ability to upload media.
Here is the solution I came up with
Here is the code on the Create Post page template:
acf_form( array(
'post_id' => 'new_post',
'new_post' => array(
'post_status' => 'publish',
'post_type' => 'post',
'post_title' => $_POST['acf']['_post_title']
),
'post_title' => true,
'post_content' => true,
'submit_value' => __('Publish', 'ppt-church'),
'updated_message' => __('Your post has been published', 'ppt-church')
));
Here is the code on the Edit Post page template:
$post_id = $_GET['post_id'];
acf_form( array(
'post_id' => $post_id,
'post_title' => true,
'post_content' => true,
'submit_value' => __('Update Post', 'ppt-church'),
'updated_message' => __('Your post has updated', 'ppt-church')
));
I use $_GET
to get the post ID from the URL
Here is the code in the functions.php
file:
// Change Post Content Type
add_filter( 'acf/get_valid_field', 'change_post_content_type');
function change_post_content_type( $field ) {
if($field['type'] == 'wysiwyg') {
$field['tabs'] = 'visual';
$field['toolbar'] = 'basic';
$field['media_upload'] = 0;
}
return $field;
}
I hope this helps anyone
The topic ‘acf_form NOT creating new Post’ is closed to new replies.
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.