WP_INSERT_POST($POST),使用UPDATE_POST_META添加一个值数组

时间:2019-10-29 作者:LovinQuaQua

我尝试通过post创建(wp\\u insert\\u post)将数组存储在多个自定义字段中。数组是$event\\u test,键是meta\\u输入上的event\\u test。

表单提交时,将显示自定义字段,但其为空。

// 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);

1 个回复
SO网友:Howdy_McGee

我建议启用错误调试。它可能会在上面的代码中显示一些PHP错误。

似乎在meta_input 您调用的数组update_post_meta() 甚至在帖子存在之前。在这些函数中,您调用$post_id 变量,而该变量似乎也不存在。

最重要的是update_post_meta() 如果可以插入,函数将返回布尔值(可能为true),如果无法更新posmeta,则返回false(空)。

相关推荐

在IS_PAGE(ARRAY())中使用变量

Similarly to this topic 我需要从用户输入中提取一些页面ID(主题选项管理页面),将它们存储在变量中,然后使用is_page() 滤器输入由用换行符分隔的数字ID组成,尽管如果我能做到这一点,我也会进行重构以允许页面插入。基于on the answer to the linked topic, 我有以下内容,但仍然不起作用。该变量输出一个正确的ID数组,但是is_page() 不基于数组进行筛选。// Wordpress my_conditional_load stuff code