Support

Account

Home Forums Front-end Issues Front-End Posting (CPT) in Genesis Reply To: Front-End Posting (CPT) in Genesis

  • The first thing is this

    
    'post_id' => 'new_post',
    

    This value triggers the ACF core pre_save_post filter to run, it runs before your filter. By default ACF inserts a “post” unless you set argument for
    'new_post => array(... https://www.advancedcustomfields.com/resources/acf_form/

    Change the post ID to 'new_band'

    and then change your login in your pre_save_post filter to

    
    	// check if this is to be a new post
    	if( $post_id != 'new_band' ) {
    		return $post_id;
    	}
    

    You should also not be adding this in the pre_save_post function

    
    	// Save the fields to the post
    	do_action( 'acf/save_post' , $post_id );
    

    acf/save_post will be triggered automatically by ACF, adding the do_action call means that it will be triggered 3 times, once here, then once before acf saves values and then again after acf saves values. And since you have set the priority of 10, acf will actually save values before your save post filter runs, here’s the exact sequence of what will happen.

    
    1) your do_action() call
    2) ACF runs it's own acf/save_post filter, with a priority of 10
    3) Your acf/save_post filter runs, also priority 10
    4) Normal ACF save is triggered by WP 'update_post' or some other WP hook
    5) ACF runs it's own acf/save_post filter, with a priority of 10
    6) Your acf/save_post filter runs, also priority 10
    

    Long story short, remove the do_action call and set the priority of your filter to <10 if you want it to run before ACF and >10 if you want it to run after ACF.

    Or even better than using acf/save_post, just add the post thumbnail to the post right in your pre_save_post filter after inserting the post.

    Here are just the related filters with the changes

    
    /**
     * Add ACF form for front end posting
     * @uses Advanced Custom Fields Pro
     */
    add_action( 'genesis_entry_content', 'tsm_do_create_post_form' );
    function tsm_do_create_post_form() {
    
    	// Bail if not logged in or able to post
    	if ( ! ( is_user_logged_in()|| current_user_can('publish_posts') ) ) {
    		echo '<p>You must be a registered author to post.</p>';
    		return;
    	}
    
    	$new_post = array(
    		'post_id'            => 'new_band', // Create a new post
    		// PUT IN YOUR OWN FIELD GROUP ID(s)
    		'field_groups'       => array(160,166), // Create post field group ID(s)
    		'form'               => true,
    		'return'             => '%post_url%', // Redirect to new post url
    		'html_before_fields' => '',
    		'html_after_fields'  => '',
    		'submit_value'       => 'Submit Post',
    		'updated_message'    => 'Saved!'
    	);
    	acf_form( $new_post );
    
    }
    
    /**
     * Back-end creation of new candidate post
     * @uses Advanced Custom Fields Pro
     */
    add_filter('acf/pre_save_post' , 'tsm_do_pre_save_post' );
    function tsm_do_pre_save_post( $post_id ) {
    
    	// Bail if not logged in or not able to post
    	if ( ! ( is_user_logged_in() || current_user_can('publish_posts') ) ) {
    		return;
    	}
    
    	// check if this is to be a new post
    	if( $post_id != 'new_band' ) {
    		return $post_id;
    	}
    
    	// Create a new post
    	$post = array(
    		'post_type'     => 'band', // Your post type ( post, page, custom post type )
    		'post_status'   => 'publish', // (publish, draft, private, etc.)
    		'post_title'    => wp_strip_all_tags($_POST['acf']['field_5815bd6e2197a']), // Post Title ACF field key
    		'post_content'  => $_POST['acf']['field_5815bdc62197b'], // Post Content ACF field key
    	);
    
    	// insert the post
    	$post_id = wp_insert_post( $post );
    
    	// ACF image field key
    	$image = $_POST['acf']['field_5815bedd2197e'];
    
    	// 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 );
    
    	return $post_id;
    
    }