$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>show custom value from frontend form in a post (custom post type)|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

show custom value from frontend form in a post (custom post type)

matteradmin8PV0评论

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');
            ?>

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');
            ?>
Share Improve this question asked Feb 16, 2019 at 1:07 LovinQuaQuaLovinQuaQua 833 silver badges19 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

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 );
Post a comment

comment list (0)

  1. No comments so far