Hi,
I have created frontend to create post in thread, I using ACF 4.4.2:
http://www.advancedcustomfields.com/resources/create-a-front-end-form/
http://www.advancedcustomfields.com/resources/using-acf_form-to-create-a-new-post/
When I upload multiple files frontend, I can not multiple select images.
How to select multiple image from Field type “Image” & attach images to post content?
This is my code:
newpost.php
<?php acf_form_head(); ?>
<?php
/*
Template Name: dangbai
*/
get_header();
?>
<div id="content">
<?php while ( have_posts() ) : the_post(); ?>
<?php
acf_form(array(
'post_id' => 'new_post',
'submit_value' => 'Đăng bài',
'field_groups' => array( 305 ),
'updated_message' => 'Đã đăng bài'
));
?>
<?php endwhile; ?>
</div>
<?php get_footer(); ?>
function.php
function my_pre_save_post( $post_id ) {
// bail early if not a new post
if( $post_id !== 'new_post' ) {
return $post_id;
}
// vars
$title = $_POST['fields']['field_5558a08e10eed'];
// Create a new post
$post = array(
'post_status' => 'publish',
'post_type' => 'post',
'post_title' => $title
);
// 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' );
/*
*Save ACF image field to post Featured Image
*/
// run before ACF saves the $_POST['fields'] data
add_action('acf/save_post', 'my_acf_save_post', 20);
function my_acf_save_post( $post_id ) {
// bail early if no ACF data
if( empty($_POST['fields']) ) {
return;
}
// specific field value
$image = $_POST['fields']['field_5558a53d6c205'];
// Bail if image field is empty
if ( empty($image) ) {
return;
}
// Add the value which is the image ID to the _thumbnail_id meta data for the current post
add_post_meta( $post_id, '_thumbnail_id', $image );
}
Thanks!