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.

Trying to get a front end post form to work with my custom post type. It posting to the post type but the 2 custom taxonomies and the tag field aren't posting/saving. Here is my CPT.

add_action('init', 'portfolio_register'); 
 function portfolio_register() {

$labels = array(
    'name' => _x('User Lists', 'post type general name'),
    'singular_name' => _x('User List', 'post type singular name'),
    'add_new' => _x('Add New', 'list item'),
    'add_new_item' => __('Add New List'),
    'edit_item' => __('Edit List Item'),
    'new_item' => __('New List Item'),
    'view_item' => __('View List Item'),
    'search_items' => __('Search Lists'),
    'not_found' =>  __('Nothing found'),
    'not_found_in_trash' => __('Nothing found in Trash'),
    'parent_item_colon' => ''
);

$args = array(
'labels' => $labels,
'public' => true, 
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'menu_icon' => get_stylesheet_directory_uri() . '/img/cutleries.png',
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'show_admin_column' => true,
'menu_position' => null,
'taxonomies' => array('post_tag', 'list_type', 'list_state'),
'supports' => array('title',
    'editor',
    'author',
    'custom-fields',
    'comments')
      );

register_post_type( 'user_lists' , $args ); }

register_taxonomy("list_type", array("user_lists"), array("hierarchical" => true, "label" => "List Types", "singular_label" => "List Type", "rewrite" => true, 'show_admin_column' => true,)); 

register_taxonomy("list_state", array("user_lists"), array("hierarchical" => true, "label" => "List State", "singular_label" => "List State", "rewrite" => true, 'show_admin_column' => true,));

And here is my form

<?php
     //EricHamby.Com
     add_shortcode('feEditor', 'feEDoShortcode');
    add_action('init', 'catch_save_form');

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

   if('POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "new_post")
{
  // Do some minor form validation to make sure there is content
if(isset ($_POST['title']))
  $title =  $_POST['title'];
else
  $errors[] = 'Please enter the wine name';

if(isset ($_POST['description']))
  $description = $_POST['description'];
else
  $errors[] = 'Please enter some notes';

$tags = $_POST['post_tags'];

    // ADD THE FORM INPUT TO $new_post ARRAY
       $new_post = array('post_title'      => stripslashes($title),
                  'post_content'    => stripslashes($description),
                  'tags_input'      => stripslashes($tags),
                  'post_status'     => 'publish', // Choose: publish, preview, future, draft, etc.
                    'post_type'       => 'user_lists', //'post',page' or use a custom post type if you want to
                  'tax_input'    => array(
                    'list_type'  => $_POST['list_type'],
                    'list_state' => $_POST['list_state'],
       )
   );

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

  //REDIRECT TO THE NEW POST ON SAVE
  $link = get_permalink( $pid );
 $_POST['form_errors'] = $errors;
 wp_redirect( $link );
 die(); 
    } // END THE IF STATEMENT
      }

   //WHERE ALL THE MAGIC HAPPENS
     function feEDoShortcode()
   {
    if(isset($_POST['form_errors']) && !empty($_POST['form_errors']))
     foreach($_POST['form_errors'] as $e)
      echo '<p class="form_error">ERROR: '.$e.'</p>';
     ?>

   <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
   <header class="entry-header">
   <h1 class="entry-title">Submit Your List!</h1>
       </header>
        <div class="entry-content">
         <form id="new_post" name="new_post" method="post" action="" class="wpcf7-form" enctype="multipart/form-data">
      <!-- post name -->
       <fieldset name="name">
       <input type="hidden" id="title" value="<?php echo date('F jS Y h:i:s A'); ?>" tabindex="5" name="title" />
      </fieldset>

  <div class="selects">
   <div class="selects1">
  <!-- list type -->
  <fieldset class="category">
    <?php erichamby_custom_taxonomy_dropdown( 'list_type', 'date', 'DESC', '10', 'list_type', '', '', 'Select List Type' ); ?>
  </fieldset>
   </div><div class="selects2">
  <!-- list state -->
  <fieldset class="category">
    <?php erichamby_custom_taxonomy_dropdown( 'list_state', 'date', 'DESC', '10', 'list_state', '', '', 'Select List State' ); ?>
  </fieldset>
  </div></div><div class="clear" ></div>

  <!-- post content -->
  <fieldset class="content space">
    <label for="description">Description and Notes:</label>
    <?php wp_editor("", 'description', array('media_buttons' => false, )); //change media_buttons to true to allow images/videos/music ?>
  </fieldset>

  <!-- post tags -->
  <fieldset class="tags space">
    <label for="post_tags">Additional Keywords (comma separated):</label>
    <input type="text" value="" tabindex="35" name="post_tags" id="post_tags" />
  </fieldset>

  <!-- submit -->
  <fieldset class="submit space">
    <input type="submit" value="Post Review" tabindex="40" id="submit" name="submit" />
  </fieldset>

  <input type="hidden" name="action" value="new_post" />
  <?php wp_nonce_field( 'new-post' ); ?>
</form>
  </div><!-- .entry-content --> 
</article><!-- #post -->
<?php
       }
        function erichamby_custom_taxonomy_dropdown( $taxonomy, $orderby = 'date', $order = 'DESC', $limit = '-1', $name, $show_option_all = null, $show_option_none = null,    $show_option_select = null ) {
   $args = array(
    'orderby' => $orderby,
    'order' => $order,
    'number' => $limit,
    'hide_empty' => 0,
      );
   $terms = get_terms( $taxonomy, $args );
  $name = ( $name ) ? $name : $taxonomy;
   if ( $terms ) {
    printf( '<select name="%s" class="postform">', $name );
    if ( $show_option_select ) {
        printf( '<option>%s</option>', $show_option_select );
    }
    if ( $show_option_all ) {
        printf( '<option value="0">%s</option>', $show_option_all );
    }
    if ( $show_option_none ) {
        printf( '<option value="-1">%s</option>', $show_option_none );
    }
    foreach ( $terms as $term ) {
        printf( '<option value="%s">%s</option>', $term->slug, $term->name       );
                       }
                                print( '</select>' );
                                  }
                }
               //EricHamby.Com
                   ?>
share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

I see two problems with your code:

First You named your custom taxonomy type which is a Reserved Term

Second You are trying to insert your taxonomy terms using 'post_category' which wont get the job done. What you can do is either create a `'tax_input' => array(' in your new post array e.g:

$new_post = array(
    'post_title'   => stripslashes($title),
    'post_content' => stripslashes($description),
    'tags_input'   => stripslashes($tags),
    'post_status'  => 'publish', // Choose: publish, preview, future, draft, etc.
    'post_type'    => 'user_lists', //'post',page' or use a custom post type if you want to
    'tax_input'    => array(
        'type'  => $_POST['type'],
        'state' => $_POST['state'],
    )
);

or you can leave your code the way it is and use wp_set_object_terms after you get the new post ID e.g:

//SAVE THE POST
$pid = wp_insert_post($new_post);
wp_set_object_terms( $pid, $_POST['type'], 'type' );
wp_set_object_terms( $pid, $_POST['state'], 'state' );

Update:

You need to make your register_taxonomy calls inside a hooked function so move then into your portfilio_register function and also give the action hooks priority e.g:

add_action('init', 'portfolio_register',100); 
function portfolio_register() {

    $labels = array(
        'name' => _x('User Lists', 'post type general name'),
        'singular_name' => _x('User List', 'post type singular name'),
        'add_new' => _x('Add New', 'list item'),
        'add_new_item' => __('Add New List'),
        'edit_item' => __('Edit List Item'),
        'new_item' => __('New List Item'),
        'view_item' => __('View List Item'),
        'search_items' => __('Search Lists'),
        'not_found' =>  __('Nothing found'),
        'not_found_in_trash' => __('Nothing found in Trash'),
        'parent_item_colon' => ''
    );

    $args = array(
    'labels' => $labels,
    'public' => true, 
    'publicly_queryable' => true,
    'show_ui' => true,
    'query_var' => true,
    'menu_icon' => get_stylesheet_directory_uri() . '/img/cutleries.png',
    'rewrite' => true,
    'capability_type' => 'post',
    'hierarchical' => false,
    'show_admin_column' => true,
    'menu_position' => null,
    'taxonomies' => array('post_tag', 'list_type', 'list_state'),
    'supports' => array('title',
        'editor',
        'author',
        'custom-fields',
        'comments')
          );

    register_post_type( 'user_lists' , $args ); 
    register_taxonomy("list_type", array("user_lists"), array("hierarchical" => true, "label" => "List Types", "singular_label" => "List Type", "rewrite" => true, 'show_admin_column' => true,)); 
    register_taxonomy("list_state", array("user_lists"), array("hierarchical" => true, "label" => "List State", "singular_label" => "List State", "rewrite" => true, 'show_admin_column' => true,));
}

add_action('init', 'catch_save_form',101);

As for your shortcode function you should return a value on a shortcode and not echo or print it out.

share|improve this answer
    
I have made the changes you suggest has you can see by the links and its still not posting. Did i do something wrong? –  Noob Theory Apr 15 '13 at 0:22
    
the reason It could the use of init hook, which can be before your post type of taxonomies are registered, try adding a late priority to it eg: add_action('init', 'catch_save_form',1000); –  Bainternet Apr 15 '13 at 7:02
    
Hmm that did fix the tag issue, however the categories still aren't working. Never have i had this much issue using CPT. –  Noob Theory Apr 15 '13 at 7:21
    
Still have not got this working, and seems no one else knows either how to get custom tax to work with front end forms. –  Noob Theory Apr 19 '13 at 4:23
    
whats your current code? both form and processing –  Bainternet Apr 19 '13 at 15:49
show 5 more comments

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.