Support

Account

Home Forums General Issues submit data from a form

Solving

submit data from a form

  • Hi all,
    I’m sure this will be easy and I’m just missing something obvious…
    I have the following code to create a custom post type that is using ACF fields:

    function generate_grading_application_cpt() {
    if (isset( $_POST['submit'] ) ) {
    
    $cpt_post_args = array(
    	'post_title'     => $_POST['full-name'],
    	'post_status'    => 'publish',
    	'post_type'      => 'grading_applications',
    );	
    	$field_key = "student_number";
    	$value = $_POST['student-number'];
    	update_field( $field_key, $value, $post_id );
    	
    	$field_key = "dojang";
    	$value = $_POST['dojang'];
    	update_field( $field_key, $value, $post_id );
    	
    	$field_key = "instructor";
    	$value = $_POST['instructor'];
    	update_field( $field_key, $value, $post_id );
    	
    	$field_key = "current_rank";
    	$value = $_POST['current-rank'];
    	update_field( $field_key, $value, $post_id );
    
    // insert the post into the database
    
    $cpt_id = wp_insert_post( $cpt_post_args, $wp_error);
    }
    };
    add_action( 'template_redirect', 'generate_grading_application_cpt' );

    The above works partially – The CPT post is created and the title & status are set correctly.
    The part I’m not getting is the ACF fields being filled with the data from the form using the update_field() command. I’m not 100% sure I’m using the command correctly so can someone advise where I might be going wrong.

    Cheers

  • You need to use field keys instead of field names to update fields when creating new posts. field_XXXXXXX

  • I tried that and didn’t have any luck there either…

    This is my code at the moment:

    function generate_grading_application_cpt() {
    if (isset( $_POST['submit'] ) ) {
    
        $formStudNum = $_POST['student-number'];
    	$formDojang = $_POST['dojang'];
    	$formInstructor = $_POST['instructor'];
    	$formCurrentRank = $_POST['current-rank'];
    
    $cpt_post_args = array(
    	'post_title'     => $_POST['full-name'],
    	'post_status'    => 'publish',
    	'post_type'      => 'grading_applications',
    );	
    // 	Student Number Field
    	$field_key = "field_5ec64e12429be";
    	$value = $formStudNum;
    	update_field( $field_key, $value, $post_id );
    	
    //  Dojang Field
    	$field_key = "field_5ec64f26429bf";
    	$value = $formDojang;
    	update_field( $field_key, $value, $post_id );
    	
    //  Instructor Field
    	$field_key = "field_5ec64f36429c0";
    	$value = $formInstructor;
    	update_field( $field_key, $value, $post_id );
    	
    //  Current Rank Field
    	$field_key = "field_5ec64f5e429c1";
    	$value = 	$formCurrentRank;
    	update_field( $field_key, $value, $post_id );
    
    // insert the post into the database
    
    $cpt_id = wp_insert_post( $cpt_post_args, $wp_error);
    }
    };
    add_action( 'template_redirect', 'generate_grading_application_cpt' );

    https://imgur.com/41uMEkJ – Screen Shot of ACF fields

    Anything obvious I’m missing. ACF fields are very new to me…

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.