0

i try to store an array in multiple custom fields via post creation (wp_insert_post). The array is $event_test and the key is event_test on the meta_input.

On form submit, the custom field is shown, but its empty.

// Form to Post Quform
add_filter('quform_post_process_1', function (array $result, Quform_Form $form) {    

    // Default Post Data   
    $content = $form->getValue('quform_1_30'); 

    // Custom Fields from the form
    $event = $form->getValue('quform_1_3');
    $event_standort = $form->getValue('quform_1_4');
    $event_budget = $form->getValue('quform_1_5');
    $event_date = $form->getValueText('quform_1_9');
    $event_time_from = $form->getValue('quform_1_10');
    $event_time_to = $form->getValue('quform_1_11');
    $event_plz = $form->getValue('quform_1_25');
    $event_stadt = $form->getValue('quform_1_24');
    $event_gaeste = $form->getValue('quform_1_26');
    $event_musiker = $form->getValue('quform_1_31');
    $event_musikstil = $form->getValue('quform_1_21');
    $event_email = $form->getValue('quform_1_28');
    $event_test = array('Value1', 'Value2', 'Value3');


    // Store all custom Data in the Posts Array
    $post = array(
        'post_title' => $event . ' in ' . $event_standort . ' (' . $event_plz . ' ' . $event_stadt . ')',
        // 'post_content' => $content,
        'post_excerpt' => $content,
        'post_type' => 'events',
        'post_status' => 'publish',

        'meta_input' => array(                          
                            // Custom Fields
                            'event_budget' => $event_budget,
                            'event_date' => $event_date,
                            'event_date_timestamp' => strtotime($event_date),
                            'event_time_from' => $event_time_from,
                            'event_time_to' => $event_time_to,
                            'event_plz' => $event_plz,
                            'event_stadt' => $event_stadt,
                            'event_gaeste' => $event_gaeste,
                            'event_musikstil' => update_post_meta($post_id, "event_musikstil", $event_musikstil),
                            'event_email' => $event_email,
                            'event_test' => update_post_meta($post_id, "event_test", $event_test),

                            // Taxonomy Names
                            'event' => $event,
                            'event_standort' => $event_standort,
                            'event_musiker' => $event_musiker
                        )
    );


    // get the post id to insert post into taxonomies and insert all fields to the post (create the post)
    $post_id = wp_insert_post($post);



    // insert taxonomy values from form
    wp_set_post_terms( $post_id, $event, 'leweb_ct_event_musiker', true ); // set the taxonomy Event Musiker from form value
    wp_set_post_terms( $post_id, $event_standort, 'leweb_ct_standort', true ); // set the taxonomy Standort from form value
    wp_set_post_terms( $post_id, $event_musiker, 'leweb_ct_musiker', true ); // set the taxonomy Musiker from form value

    return $result;
}, 10, 2);
  • Why are you passing update_post_meta to event_test and not $event_test, like the others? – Jacob Peattie Oct 29 '19 at 11:26
  • Because its an array, like you can see above – LovinQuaQua Oct 29 '19 at 11:57
  • And? That doesn't mean you need to use update_post_meta(). You should just pass the array as the value. update_post_meta() is going to fail because the post doesn't exist yet, meaning that it's setting the value to false. – Jacob Peattie Oct 29 '19 at 12:08
  • when i pass the array as value, it is stored in the database, but its not stored as custom field where the values are separated... – LovinQuaQua Oct 29 '19 at 12:50
  • If you want them separated then each item in the array needs to be a separate meta key. – Jacob Peattie Oct 29 '19 at 12:53
0

I would suggest turning on error debugging. It will likely show you some PHP errors in the above code.

It appears that in the meta_input array you're calling things like update_post_meta() before the post even exists. In those functions you're calling the $post_id variable and that also doesn't appear to exist.

On top of that, the update_post_meta() function will return a boolean ( likely true ) if it can insert, or false ( empty ) if it cannot update the postmeta.

| improve this answer | |

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.