0

I am using a frontend form to create posts in a custom post type (Everything is working fine excepts custom fields). I tried for hours to figure out, how i can show custom field value from the form in the post.

this is how i put the field values in the post.

    add_filter('quform_post_process_1', function (array $result, Quform_Form $form) {    
    $title = $form->getValue('quform_1_22');    
    $content = $form->getValue('quform_1_23'); 
    $add = $form->getValue('quform_1_24');

    $post = array(
        'post_title' => $title,
        'post_content' => $content,
        'post_type' => 'events',
        'post_status' => 'publish',
        'post_custom_field' => $add // this is the custom field which is not working
    );

    wp_insert_post($post);

    return $result;
}, 10, 2);

When i try to show a custom field like 'post_custom_field' its not working.

This code is within the archive.php for my custom post type

        <?php while ( have_posts() ) : ?>
            <?php the_post(); ?>
            <?php
                the_content();
                echo get_post_field('post_custom_field');
            ?>
1

Refer to the parameters section in the documentation for wp_insert_post.

Custom fields (meta data) must be passed in the form of a key/value array with the parameter name 'meta_input'.

Change

'post_custom_field' => $add

to

'meta_input' => array( 'post_custom_field' => $add )

To show the value, use the get_post_meta function. get_post_field is only for the native fields in the posts table, not for custom meta data.

echo get_post_meta( get_the_ID(), 'post_custom_field', true );

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.