0

I'm attempting to have Gravity Forms create two post types in one form, based on data in the form.

The two post type slugs are teardown and engine, and it's successful in creating both posts, but none of the custom fields form data is passing to the database. I'm left with an empty post. However, I am getting notifications from GF submission notifications showing the form data in the email. For some reason it isn't passing through to the CPT post when I view in the admin panel.

Why is the form data not populating the custom fields? It seems like there is a disconnect with the $entry[]?

add_action( 'gform_after_submission_1', 'after_submission', 10, 2 );
function after_submission() {
    // TEARDOWN POST TYPE STARTS HERE
        $td_post_args = array(
        'comment_status' => 'closed',
        'ping_status' => 'closed',
        'post_author' => $user_ID,
        'post_title' => $entry[1],
        'post_status' => 'draft', 
        'post_type' => 'teardown',                                 
    );
    $post_id = wp_insert_post( $td_post_args );

    // TEARDOWN CPT META VALUES
    $td_meta_values = array(
        'wpcf-td-mfg' => $entry[2],
        'wpcf-td-model' => $entry[3],
        'wpcf-td-msn' => $entry[4],
        'wpcf-td-location' => $entry[7],
        'wpcf-td-tail-number' => $entry[5],
        'wpcf-td-last-operator' => $entry[6],
    );
    if ( $post_id > 0 ) {
        foreach ( $td_meta_values as $key => $value ) {
            update_post_meta( $post_id, $key, $value );
        }
    }

    // ENGINE POST TYPE STARTS HERE 
    $engine_post_args = array(
        'comment_status' => 'closed',
        'ping_status' => 'closed',
        'post_author' => $user_ID,
        'post_title' => $entry[1],
        'post_status' => 'draft', 
        'post_type' => 'engine',                     
    );
    $post_id = wp_insert_post( $engine_post_args );
    $engine_meta_values = array(
        'wpcf-td-engine-model' => $entry[11],
        'wpcf-td-engine-pn' => $entry[13],
        'wpcf-td-engine-qty' => $entry[14],
    );
    if ( $post_id > 0 ) {
        foreach ( $engine_meta_values as $key => $value ) {
            update_post_meta( $post_id, $key, $value );
        }
    }
}

Credit: This code is based on a super helpful response from GhostToast

  • perhaps $post_id is not greater than 0? If the post is created but the custom fields are not added, the problem might be with the foreach loop or the if wrapping it. – gdaniel Jun 5 '15 at 21:45
  • But shouldn't that function return an integer if the function successfully created a post? – CA_ Jun 6 '15 at 2:09
0

I know this is an old post, but a Google search brought me here.

Where are you getting $user_ID and your array $entry ? They are not defined anywhere in your functions.

Where does $entry come from? I know HOW to get the user_ID, but you not doing so anywhere in this code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.