自定义分类不保存在前端帖子中

时间:2014-05-26 作者:bilcker

我正在尝试创建一个前端张贴表单,允许用户在学校关闭时创建张贴。我有4个自定义分类法:关闭学校、学习开放、安全到达、小时延迟。每个学校都有30所左右的学校。一旦帖子提交,我想显示关闭、开放、延迟等的学校。

如果我在后端构建帖子,它就会工作,并显示出我想要的一切。如果我从前端发帖,没有显示任何类别,因此我知道我存储分类法的方式有问题,但我尝试了许多论坛和技术,但没有成功。非常感谢您的帮助,谢谢。

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(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\');
}

}
我的表格是自己的

<form id="new_post" name="new_post" method="post" action="" class="wpcf7-form front-end-form" enctype="multipart/form-data">
<!-- post name -->
    <fieldset name="name">
        <label for="title" class="selection-title">Area and/or Region:</label>
        <!--<input type="text" id="title" value="" tabindex="5" name="title" />-->
        <select name="title" id="title">
            <option value="">-Please select an area and/or region-</option>
        </select>
    </fieldset>

    <!-- post Category -->
    <fieldset class="category">
        <label for="cat" class="selection-title">Reason For closure and/or delay:</label>
        <?php wp_dropdown_categories(array(\'hide_empty\' => 0, \'name\' => \'cat\', \'orderby\' => \'name\', \'selected\' => $category->parent, \'hierarchical\' => true, \'show_option_none\' => __(\'Select an area and/or region\'))); ?>
    </fieldset>
    <!-- post Content -->

     <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>
  <fieldset>
      <label for="study_open" class="selection-title">Open for Study Only:</label>
      <?php
        $study_only = get_terms(\'study_open\', \'orderby=id&hide_empty=0\');
        $counter = 0;
        foreach ($study_only as $study) {
            $counter++;
            $option = \'<label for="\'.$study->slug.\'">\'.$study->name.\'</label>\';
        $option .= \'<input type="checkbox" name="terms[]" id="\'.$study->slug.\'" value="\'.$study->slug.\'">\';
        echo $option;
        }
    ?>
   </fieldset>
   <fieldset>
       <label for="open_for_safe_arrival" class="selection-title">Open for Safe Arrivals Only:</label>
       <?php
        $safe_arrival = get_terms(\'safe_arrival\', \'orderby=id&hide_empty=0\');
        $counter = 0;
        foreach ($safe_arrival as $safe) {
            $counter++;
            $option = \'<label for="\'.$safe->slug.\'">\'.$safe->name.\'</label>\';
            $option .= \'<input type="checkbox" name="terms[]" id="\'.$safe->slug.\'" value="\'.$safe->slug.\'">\';
            echo $option;
        }
        ?>
     </fieldset>
     <fieldset>
          <label for="2_hour_delay" class="selection-title">2 Hour Delay:</label>
          <?php
            $hour_delay = get_terms(\'hour_delay\', \'orderby=id&hide_empty=0\');
        $counter = 0;
        foreach ($hour_delay as $delay) {
            $counter++;
            $option = \'<label for="\'.$delay->slug.\'">\'.$delay->name.\'</label>\';
            $option .= \'<input type="checkbox" name="terms[]" id="\'.$delay->slug.\'" value="\'.$delay->slug.\'">\';
            echo $option;
        }
        ?>
    </fieldset>


    <fieldset class="content">
        <label for="description" class="selection-title">Description and Notes:</label>
        <textarea id="description" tabindex="15" name="description" cols="80" rows="10"></textarea>
        </fieldset>

        <fieldset class="submit">
            <button type="submit" value="Submit Cancellation/Closure" name="submit" tabindex="40" id="submit"><span>Submit Cancellation/Closure</span></button>
            <!--<input type="submit" value="Submit Cancellation/Closure" tabindex="40" id="submit" name="submit" />-->
        </fieldset>
        <input type="hidden" name="action" value="new_post" />
        <?php wp_nonce_field( \'new-post\' ); ?>
  </form>

1 个回复
SO网友:bilcker

经过多次尝试和错误后,我发现了这一点。对$new\\u post数组进行了一些细微的更改。

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[\'safe_arrival\'], \'hour_delay\' => $_POST[\'hour_delay\']), //Place all of our custom taxonomies into the posted variable terms as an array
        \'post_status\'   =>  \'publish\', // Choose: publish, preview, future, draft, etc.
        \'post_type\' =>  \'post\',  //\'post\',page\' or use a custom post type if you want to
    );
还要在每个自定义分类法上使用wp\\u set\\u object\\u terms而不是wp\\u set\\u post\\u 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\');

结束

相关推荐

Taxonomy vs Post Status

我正在构建一个web应用程序,因此可扩展性是我开发时的主要关注点之一。在我正在构建的应用程序上,我有一个名为“项目”的帖子类型。项目可以是:我不需要任何类型的UI,只需要能够查询处于一种或多种状态的所有项目。我假设,由于post\\u status位于posts表中,使用此方法执行这些查询比使用分类法更快?所以,我的问题是,如果有100000个项目,查询哪个会更快?发布状态还是分类法?干杯