I’m trying to use ACF Pro to create a form that people can submit, which should then create a draft post.
The ACF Field Group is there and renders on the page.
Upon submitting the form, it does display a positive confirmation message but no post has been created.
What am I doing wrong?
I have created a custom theme with the following file:
<?php
acf_form_head();
get_header();
?>
<div id="content">
<div class="wrap">
<form id='post' class='acf-form' action='' method='post'>
<?php
acf_form([
'field_groups' => ['group_5***d'],
'post_id' => 'new_post',
'post_title' => false,
'post_content' => false,
'post_category' => 'academy',
'form' => false,
'submit_value' => __("Submit post", 'acf'),
'updated_message' => __("Thank you!", 'acf'),
'html_updated_message' => '<div id="message" class="updated"><p>%s</p></div>'
]);
if(current_user_can('activate_plugins')) {
acf_form([
'field_groups' => ['group_5***b'],
'post_id' => 'new_post',
'post_category' => 'academy',
'form' => false
]);
}
?>
<div class="acf-form-submit">
<input type="submit" class="acf-button" value="Submit post">
<span class="acf-spinner"></span>
</div>
</form>
</div>
</div>
<?php get_footer();
And I have the following code snippet:
function savePostsAsAcademyDraft($post_id) {
$term = get_term_by('slug', 'academy', 'category');
wp_update_post([
'ID' => $post_id,
'post_status' => 'draft',
'post_category' => [/*$term->id*/3]
]);
return $post_id;
}
add_filter('acf/pre_save_post' , 'savePostsAsAcademyDraft', 10, 1);
I don’t know why it’s not working, but here are some problems with your code that may or may not be causing issues.
Instead of using 2 acf_form() calls you should have only one and construct the field groups to be included before you call acf_form()
$field_groups = array('group_5***d');
if (current_user_can('activate_plugins')) {
$field_groups[] = 'group_5***b';
}
// in your acf_form() call
....
'field_groups' => $field_groups;
....
Doing the above will also eliminate the need to call acf_form() with 'form' => false
There is no post_category argument for acf_form()
To set post values use the argument “new_post”
.....
'post_id' => 'new_post',
'new_post' => array(
// wp_insert_post() arguments here
// https://developer.wordpress.org/reference/functions/wp_insert_post/
),
With the above in place there will be no need to get the term and set it in your acf/pre_save_post filter. You should be able to set it to draft by supplying just the ID and status to wp_update_post()
Another issue you might be having is not supplying a post title. Even if you do not supply a post title field in the form you should be setting something here and include it in the new_post argument array. WP has been know to have issue when a post is saved without a title.
https://www.advancedcustomfields.com/resources/acf_form/
Thank you John,
I have simplified my code to the one below. However, upon form submit the WordPress site crashes. Is there anything wrong with this code?
<?php
acf_form_head();
get_header();
?>
<div id="content">
<div class="wrap">
<form id='post' class='acf-form' action='' method='post'>
<?php
$field_groups = array('group_596925d98777d');
acf_form([
'field_groups' => $field_groups,
'post_id' => 'new_post',
'new_post' => array(
'post_category' => 'academy',
'post_status' => 'draft',
'post_title' => $post_id,
),
'post_content' => false,
'form' => false,
'submit_value' => __("Submit post", 'acf'),
'updated_message' => __("Thank you!", 'acf'),
'html_updated_message' => '<div id="message" class="updated" style="display:block;"><p>%s</p></div>'
]);
?>
</form>
</div>
</div>
<?php get_footer();
You must be logged in to reply to this topic.
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.