Reputation: 3613
I added this to my function.php for saving the post:
function my_pre_save_post( $post_id )
{
// check if this is to be a new post
if( $post_id != 'new' )
{
return $post_id;
}
// Create a new post
$post = array(
'post_status' => 'draft' ,
'post_title' => 'A title, maybe a $_POST variable' ,
'post_type' => 'post' ,
);
// insert the post
$post_id = wp_insert_post( $post );
// update $_POST['return']
$_POST['return'] = add_query_arg( array('post_id' => $post_id), $_POST['return'] );
// return the new ID
return $post_id;
}
add_filter('acf/pre_save_post' , 'my_pre_save_post' );
And on the page.php I've got this code:
$postid = get_the_ID();
if($postid ==50){ //50 is a Page I created for the Form
$options = array(
'post_id' => 'new',//$post->ID, // post id to get field groups from and save data to
'field_groups' => array(46), // this will find the field groups for this post (post ID's of the acf post objects)
'form' => true, // set this to false to prevent the <form> tag from being created
'form_attributes' => array( // attributes will be added to the form element
'id' => 'post',
'class' => '',
'action' => '',
'method' => 'post',
),
'return' => add_query_arg( 'updated', 'true', get_permalink() ), // return url
'html_before_fields' => '', // html inside form before fields
'html_after_fields' => '', // html inside form after fields
'submit_value' => 'Update', // value for submit field
'updated_message' => 'Post updated.', // default updated message. Can be false to show no message
);
acf_form( $options );
}
Now, I think the saving was successfull, how do I get the whole Posts that have been created with this Form?
I tried this (on page with ID 51), but I get nothing:
$posts = get_posts(array(
'post_type' => 'event',
'posts_per_page' => -1,
'meta_key' => 'location',
'meta_value' => 'melbourne'
));
if($posts)
{
foreach($posts as $post)
{
the_field('titel');
}
}
The ACF Plugin for Wordpress is very well documented, but I couldnt solve my problem. http://www.advancedcustomfields.com/resources
Upvotes: 2
Views: 2227
Reputation: 178
according to the ACF form field documentation https://www.advancedcustomfields.com/resources/acf_form/ the "return" field has 2 dynamic placeholders
"(String) The URL to be redirected to after the form is submitted. Defaults to the current URL with a GET parameter ‘?updated=true’. A special placeholder ‘%post_url%’ will be converted to post’s permalink. A special placeholder ‘%post_id%’ will be converted to post’s ID."
if you add 'return' => '?newpost=%post_id%',
for example, you get back your original page with the new post id as URL parameter that you can handle with PHP in your page template
Upvotes: 2
Reputation: 1992
The documentation says you've to set the post_id
value inside your $options
array to new_post
instead of new
if you want to create a new post. Also you've to use the new_post
array key with an array with data for the new post as value for the key.
Check out the acf_form documentation. This is what I basically mean:
$options = array(
'post_id' = 'new_post',
'new_post' = array(
//new post data
)
);
Upvotes: 0