在自定义表单中保存自定义帖子

时间:2013-10-09 作者:Joren

我想在自定义表单中保存自定义帖子。当我提交表单时,所有数据都会得到处理,然后我需要将其保存到db。我正在尝试使用update_post_meta, 但这需要一个我当时没有的帖子id。我怎样才能得到呢?还是应该使用不同的方法保存数据?

此外,在保存来自wp admin的自定义帖子时,我钩住了save 功能到edit_post. 因为这是一个自定义表单,所以没有默认挂钩。我假设没有元数据的帖子本身会自动保存在那里,那么我是否需要在自定义表单的save函数中复制此功能?

因此,我实际上想知道如何从头开始保存自定义帖子(及其元数据)(没有任何挂钩和帖子id)。

代码:

function dating_user_create_account_validate() {
    $errors = array();

    // Truth verification
    if(!isset($_POST[\'dating_user_truth_field\']))
        $errors[] = "Je moet aangeven dat je de gegevens naar waarheid hebt ingevuld. Vink het selectievakje onderaan het formulier aan.";

    // Empty field validation
    $fields = array(\'email\',\'gender\',\'sexuality\',\'day\',\'month\',\'year\',\'region\',\'city\');
    $unset_fields = array();
    foreach($fields as $field)
        if(!isset($_POST[\'dating_user_\'.$field.\'_field\']) || $_POST[\'dating_user_\'.$field.\'_field\'] === \'\')
            $unset_fields[] = $field;

    function alias($k) {
        $aliases = array(
            \'email\'     => \'E-mailadres\',
            \'password\'  => \'Wachtwoord\',
            \'gender\'    => \'Geslacht\',
            \'sexuality\' => \'Geaardheid\',
            \'birthday\'  => \'Geboortedatum\',
            \'region\'    => \'Regio\',
            \'city\'      => \'Stad\',
        );
        return $aliases[$k]; 
    }
    if(sizeof($unset_fields) > 0) {
        $errors[] = implode(\', \', array_map(\'alias\', $unset_fields)) . \' niet ingevuld.\';
        return $errors;
    }

    // Email validation
    if(!is_email($_POST[\'dating_user_email_field\']))
        $errors[] = \'Vul a.u.b. een geldig E-mailadres in.\';

    // Nonce verification
    $nonces = array(\'email\',\'gender\',\'sexuality\',\'birthday\',\'region\',\'city\');
    foreach($nonces as $nonce) 
        if(!wp_verify_nonce($_POST[\'dating_user_\'.$nonce.\'_box_nonce\'],\'dating_user_\'.$nonce.\'_box\'))
            die($nonce." nonce ongeldig");

    // Duplicate email check
    $mail_args = array( 
           \'post_type\'      => \'dating_user\', 
           \'meta_key\'       => \'_email_meta_value_key\', 
           \'meta_value\'     => $_POST[\'dating_user_email_field\'],
           \'posts_per_page\' => 10,
    );
    $mail_res = new WP_Query($mail_args);
    if($mail_res->have_posts()) {
        $errors[] = \'Dit E-mailadres is al in gebruik. Kies een andere of vind je wachtwoord terug bij Account > Wachtwoord vergeten.\';
        return $errors;
    }

    $password = generateRandomString(15);

    $final_data = array(
        \'email\'     => sanitize_text_field( $_POST[\'dating_user_email_field\'] ),
        \'password\'  => wp_hash_password($password),
        \'gender\'    => $_POST[\'dating_user_gender_field\'],
        \'sexuality\' => $_POST[\'dating_user_sexuality_field\'],
        \'birthday\'  => $_POST[\'dating_user_day_field\'].\'-\'.$_POST[\'dating_user_month_field\'].\'-\'.$_POST[\'dating_user_year_field\'],
        \'region\'    => $_POST[\'dating_user_region_field\'],
        \'city\'      => $_POST[\'dating_user_city_field\'],
    );

    //so I\'ve got no $post_id
    foreach($final_data as $data_key => $data_value)
        update_post_meta($post_id, \'_\'.$data_key.\'_meta_value_key\', $data_value);

    send_email($final_data[\'email\'], $password);

    header(\'Location: \'.$_SERVER[\'PHP_SELF\'].\'/account-aangemaakt/\');
}

1 个回复
最合适的回答,由SO网友:Sagive 整理而成

这并不是一个真正的答案,但更多的是解释了东西是如何工作的。

What do you mean no post id?
Think about it, you want to update a post meta but which post?

如果您试图创建一个新帖子,您需要一个不同的代码<如果你想更新一篇特定的帖子,你必须告诉wordpress更新哪一篇,这意味着你必须附上帖子ID。

update_post_meta function clearly states that the ID is required.

因为我很确定你实际上想从Scrath创建一个新帖子,而不是更新现有帖子,这里是一个例子。

$post = array(
    \'post_title\'    => \'some title\',
    \'post_status\'   => \'publish\',
    \'post_type\' => \'custom_post_name\'
);

$new_post_id = wp_insert_post($post, 10, 1);

// Do the wp_insert_post action to insert it
do_action(\'wp_insert_post\', \'wp_insert_post\', 10, 1); 
update_post_meta($new_post_id, \'some_meta_field\', \'some_data\');

结束

相关推荐