Support

Account

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

Solved

Front-End Posting (CPT) in Genesis

  • Hi everyone!

    I am not able to create a custom post on the front end. The title and content wont be saved. It is ‘draft’ instead of ‘published’ and its a ‘post’ instead of my custom post type. A repeater field (single line text) doesnt work either, its just and edit field. The only thing that works is the featured image.

    My idea is to have a page template that contains the front-end form.
    The user should be able to create that post by entering all fields in that form and when coming back to that page he should be able to change the information that he previously entered.

    I am using Genesis and followed this tutorial I found in the internet:
    http://thestizmedia.com/front-end-posting-with-acf-pro/

    And I found this thread in the acf-forum dealing with a problem following that same tutorial:
    https://support.advancedcustomfields.com/forums/topic/help-with-new-post-from-fontend/

    This is the code I am using:

    
    <?php
    /**
     * Template Name: Create Post
     *
     * @uses Advanced Custom Fields Pro
     */
    
    /**
     * Add required acf_form_head() function to head of page
     * @uses Advanced Custom Fields Pro
     */
    add_action( 'get_header', 'tsm_do_acf_form_head', 1 );
    function tsm_do_acf_form_head() {
    	// Bail if not logged in or not able to post
    	if ( ! ( is_user_logged_in() || current_user_can('publish_posts') ) ) {
    		return;
    	}
    	acf_form_head();
    }
    
    /**
     * Deregister the admin styles outputted when using acf_form
     */
    add_action( 'wp_print_styles', 'tsm_deregister_admin_styles', 999 );
    function tsm_deregister_admin_styles() {
    	// Bail if not logged in or not able to post
    	if ( ! ( is_user_logged_in() || current_user_can('publish_posts') ) ) {
    		return;
    	}
    	wp_deregister_style( 'wp-admin' );
    }
    
    /**
     * 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_post', // 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_post' ) {
    		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 );
    
    	// Save the fields to the post
    	do_action( 'acf/save_post' , $post_id );
    
    	return $post_id;
    
    }
    
    /**
     * Save ACF image field to post Featured Image
     * @uses Advanced Custom Fields Pro
     */
    add_action( 'acf/save_post', 'tsm_save_image_field_to_featured_image', 10 );
    function tsm_save_image_field_to_featured_image( $post_id ) {
    
    	// Bail if not logged in or not able to post
    	if ( ! ( is_user_logged_in() || current_user_can('publish_posts') ) ) {
    		return;
    	}
    
    	// Bail early if no ACF data
    	if( empty($_POST['acf']) ) {
    		return;
    	}
    
    	// 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 );
    
    }
    
    // acf/update_value/name={$field_name} - filter for a specific field based on it's key
    
    // Run the Genesis loop
    genesis();
    

    I hope that someone sees the problem or can give suggestions.

    Thank you!

  • 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;
    
    }
    
    
  • Hello John Huebner!

    Thank you so much for your help and detailed explanation!

    I changed the filters and removed the action for saving the image.

    I works perfectly!

  • I would like that when the user comes back to the same page, the values entered before will be loaded and when posting again the post will be updated instead of creating a new post. So it means that the user will only be able to have one post and he is always able to update.

    What is the best practice for laoding those field and making sure if a post already exist?

    All ideas are appreciated!!

    Thanks!

  • To edit a post that already exists you would add the acf_form() code to the template that shows the post and instead of using “new_band” for the post ID you would us the current post id

    
    'post_id' => $post->ID,
    

    or just not include that argument at all and ACF will automatically use the current post ID.

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

The topic ‘Front-End Posting (CPT) in Genesis’ is closed to new replies.