Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to create a front end posting form that will allow users to create a post when schools are closed. I have 4 custom taxonomies closed_schools, study_open, safe_arrival, hour_delay. each has the same list of 30 or so schools. Once the post is submitted I want to display the schools that are closed, opened, delayed etc.

If I build the post in the back end it works, and shows everything like I intend. If I post from the front end none of the categories show up, So I know there is something wrong with how I am storing the taxonomies but I have tried many forums and techniques but no luck. Any help would be super appreciated, thanks.

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

}

My Form Istelf

<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>
share|improve this question

1 Answer 1

I figured this out after much trial and error. Mad some slight changes to the $new_post array.

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

Also to use wp_set_object_terms instead of wp_set_post_terms on each custom taxonomy

//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');
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.