WP前端表单不保存我的自定义分类

时间:2014-05-26 18:52:21

标签: wordpress forms taxonomy custom-taxonomy

我正在尝试通过wordpress创建一个前端发布表单,这将允许用户在学校关闭时创建帖子。我有4个自定义分类,closed_schools,study_open,safe_arrival,hour_delay。每个都有相同的3o左右的学校名单。问题是,在创建帖子后,我无法显示在该类别下选择了哪些学校。如果我在后端构建帖子它可以工作,而不是从前端,所以我知道我存储分类法的方式有问题。任何建议都非常欢迎,谢谢

我的前端表格模板

if(isset($_POST['submit'])){
    $title =  $_POST['title'];
    $description = $_POST['description'];
    $category = $_POST['cat'];
    $tags = $_POST['post_tags'];
    $terms = $_POST['terms'];

    $error_msg = array();
    if($title == ''){
        $error_msg[] = 'Please select an area and/or region.';
    }
    if($description == ''){
        $error_msg[] = 'Please write a description for the closure and/or delay.';
    }
    if($category == '-1'){
        $error_msg[] = 'Please identify the reason for the closure and/or delay.';
    }
    else if( !$error_msg && 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "new_post"){
        $new_post = array(
        'post_title'    =>  $title,
        'post_content'  =>  $description,
        'post_category' =>  array($_POST['cat']),  // Usable for custom taxonomies too
        'tags_input'    =>  array($tags),
        'tax_input' => array(
            'closed_schools' => $_POST['terms'], 
            'study_open' => $_POST['terms'], 
            'safe_arrival' => $_POST['terms'], 
            'hour_delay' => $_POST['terms'],
        ),
        'post_status'   =>  'publish',           // Choose: publish, preview, future, draft, etc.
        'post_type' =>  'post',  //'post',page' or use a custom post type if you want to
    );

    //SAVE THE POST
    $pid = wp_insert_post($new_post);

    //SET OUR TAGS UP PROPERLY
    wp_set_post_tags($pid, $_POST['post_tags']);

    //SET OUR POST TERMS, CUSTOM TAXONOMIES
    wp_set_post_terms($pid, $_POST['terms'], 'closed_schools', 'safe_open', 'safe_arrival', 'hour_delay', true);

    //REDIRECT TO THE NEW POST ON SAVEs
    $link = get_permalink( $pid );
    wp_redirect( $link );
    //POST THE POST
    do_action('wp_insert_post', 'wp_insert_post');
}

}

在表单中,我在4个分类中的复选框中显示学校,如此。我只是更改了我正在展示的每个分类的名称。

<fieldset>
<label for="closed_schools" class="selection-title">Closed Schools:</label>
<?php
    $closed_schools = get_terms('closed_schools', 'orderby=id&hide_empty=0');
    $counter = 0;
    foreach ($closed_schools as $close) {
        $counter++;
        $option = '<label for="'.$close->slug.'">'.$close->name.'</label>';
        $option .= '<input type="checkbox" name="terms[]" id="'.$close->slug.'" value="'.$close->slug.'">';
        echo $option;
    }                   
?>
</fieldset>

最后,我在loop.php和single.php

上显示输出
    <?php echo get_the_term_list( $post->ID, 'closed_schools', '<strong>Closed Schools:</strong> ', ', ', '' );  ?>
    <?php echo get_the_term_list( $post->ID, 'study_open', '<strong>Open For Study Purposes Only:</strong> ', ', ', '' );  ?>
    <?php echo get_the_term_list( $post->ID, 'safe_arrival', '<strong>Open for Students Who Can Ariive Safely:</strong> ', ', ', '' );  ?>
    <?php echo get_the_term_list( $post->ID, 'hour_delay', '<strong>2 Hour Delay:</strong> ', ', ', '' );  ?> 

1 个答案:

答案 0 :(得分:0)

我明白了!这是我的答案。

我需要为每个使用wp_set_object_terms而不是wp_set_post_terms而且它有效

//SET OUR POST TERMS, CUSTOM TAXONOMIES
wp_set_object_terms($pid, $_POST['terms'], 'closed_schools');
wp_set_object_terms($pid, $_POST['terms'], 'study_open');
wp_set_object_terms($pid, $_POST['terms'], 'safe_arrival');
wp_set_object_terms($pid, $_POST['terms'], 'hour_delay');