【问题标题】:WP Front End Form Not Saving My Custom TaxonomiesWP 前端表单不保存我的自定义分类
【发布时间】:2014-05-26 18:52:20
【问题描述】:

我正在尝试通过 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> ', ', ', '' );  ?> 

【问题讨论】:

    标签: wordpress forms taxonomy custom-taxonomy


    【解决方案1】:

    我想通了!这是我的答案。

    我需要为每个人使用 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');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-22
      • 1970-01-01
      • 2017-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-11
      相关资源
      最近更新 更多