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
1 Answer
Reset to default 2Refer 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 );