Support

Account

Home Forums Front-end Issues Select multiple images and attach to post content

Helping

Select multiple images and attach to post content

  • 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!

  • I think I may be a little confused about what you’re trying to do.

    An ACF Image field only allows selecting a single image. And as far as I know a post can only have one post_thumbnail add_post_meta( $post_id, '_thumbnail_id', $image );

    To select multiple images you’d need to use a repeater to allow adding multiple images in the form.

    If you want to add multiple attachments to a post then you should look at https://codex.wordpress.org/Function_Reference/wp_insert_attachment.

    If I’m misunderstanding please explain a bit more about what it is you’re trying to do.

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Select multiple images and attach to post content’ is closed to new replies.