Support

Account

Home Forums Front-end Issues Using Separate ACF Fields Inside Custom Front End Submission Form

Unread

Using Separate ACF Fields Inside Custom Front End Submission Form

  • Hello!

    Really looking forward to your assistance, the ACF community.
    The issue seems to be so simple, but still struggling to solve it.

    So, I’m dealing with WP Pro Real Estate 7 theme (http://contempothemes.com/wp-real-estate-7/), there’s implemented default frontend ‘List A Property’ form. I’m adding only 2 ACF fields inside it and need them to pass their values during the post saving process.

    But, the thing is that I was able to manage things for the ‘edit post’ custom page template, but not for the ‘save post’.

    For ‘edit post’ this code inside template-edit-listing.php (custom page template) worked out really well:

     acf_form (array(
     'post_id'   => $current_post, 
     'field_groups' => array(3170), 
     'form' => false,
     'return' => '', 
     'post_title' => false,
     'post_content' => false,
     ));

    I was trying to use it for the ‘save post’, changing ‘post_id’ in many various combinations, but it doesn’t work at all. There were already:

    'post_id' => $_POST['acf'],

    with the ACF documentation’s code inside functions.php:

    `function my_acf_save_post( $post_id ) {

    // bail early if no ACF data
    if( empty($_POST[‘acf’]) ) {
    return;
    }

    // array of field values
    $fields = $_POST[‘acf’];

    // specific field value
    $field = $_POST[‘acf’][‘field_5966915177626’];

    }`

    and

    $post_id = get_the_ID();
    acf_form (array(
    	'post_id'   => $post_id,
        ...

    and

    'post_id' => 'new'

    with the code inside functions.php:

    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' ) {
            return $post_id;
        }
     
        // Create a new post
        $post = array(
            'post_type'     => 'listings', // Your post type ( post, page, custom post type )
            'post_status'   => 'publish', // (publish, draft, private, etc.)
            'post_title'    => '', // Post Title ACF field key
            'post_content'  => '', // 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;
     
    }

    but any positive result so far.

    Could somebody assist me with fixing this issue, please?

    Best wishes!

Viewing 1 post (of 1 total)

The topic ‘Using Separate ACF Fields Inside Custom Front End Submission Form’ is closed to new replies.