admin管理员组文章数量:1122846
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);
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);
Share
Improve this question
asked Oct 29, 2019 at 11:23
LovinQuaQuaLovinQuaQua
733 silver badges18 bronze badges
5
|
1 Answer
Reset to default 0I 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.
本文标签: wpinsertpost(post)add an array of values with updatepostmeta
版权声明:本文标题:wp_insert_post($post), add an array of values with update_post_meta 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736286217a1927623.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
update_post_meta
toevent_test
and not$event_test
, like the others? – Jacob Peattie Commented Oct 29, 2019 at 11:26update_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 tofalse
. – Jacob Peattie Commented Oct 29, 2019 at 12:08