I'm having problems trying to create a custom type post with taxonomies using a form at the Front End.

I have tried to add several lines of code and use 'tax_input' but I always get errors.

The custom post type is: pet and the taxonomies are pet-category and pet-city. This code is working fine but is only posting the title, and the content.

Please if somebody could tell me what I need to add in order to also post this two taxonomies wold be appreciated. but I have tried and tried without success,

<?php /* Template Name: Insert Posts */

$postTitleError = '';

if(isset($_POST['submitted']) && isset($_POST['post_nonce_field']) && wp_verify_nonce($_POST['post_nonce_field'], 'post_nonce')) {

$postTitle = trim($_POST['postTitle']);

$new_post = array(
'post_title' => wp_strip_all_tags( $_POST['postTitle'] ),
'post_content' => $_POST['postContent'],
'post_type' => 'pet',
'post_status' => 'publish'


$post_id = wp_insert_post($new_post,$wperror);


// Update Custom Meta
update_post_meta($post_id, 'vsip_custom_one', esc_attr(strip_tags($_POST['customMetaOne'])));
update_post_meta($post_id, 'vsip_custom_two', esc_attr(strip_tags($_POST['customMetaTwo'])));

// Redirect
wp_redirect( home_url() ); exit;

} ?>

<?php get_header(); ?>    

<!-- #primary BEGIN -->
<div id="primary">
<form action="" id="primaryPostForm" method="POST">
<!-- Post Title -->
<label for="postTitle"><?php _e('Post\'s Title:', 'framework') ?></label>
<input type="text" name="postTitle" id="postTitle" value="<?php if(isset($_POST['postTitle'])) echo $_POST['postTitle'];?>" class="required" />

<?php if($postTitleError != '') { ?>
<span class="error"><?php echo $postTitleError; ?></span>
<div class="clearfix"></div>
<?php } ?>

<!-- Post Content -->
<label for="postContent"><?php _e('Post\'s Content:', 'framework') ?></label>
<textarea name="postContent" id="postContent" rows="8" cols="30"><?php if(isset($_POST['description'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['description']); } else { echo $_POST['description']; } } ?></textarea>

<!-- Pet Category -->
<label for="pet-category">Categoria: *</label>
<select name="pet_category" id="pet_category" tabindex="9" class="required">
<option value=""></option>
$categories = get_terms('pet-category', array('hide_empty' => 0));
foreach ($categories as $category) {
echo "<option id='pet_category' value='$category->slug'>$category->name</option>";

<!-- Pet City -->
<label for="pet-city">City: *</label>
<select name="pet_city" id="pet_city" tabindex="9" class="required">
<option value=""></option>
$cities = get_terms('pet-city', array('hide_empty' => 0));
foreach ($cities as $city) {
echo "<option id='pet_city' value='$city->slug'>$city->name</option>";

<!-- Pet Image -->
<fieldset name="site-image" class="site-image">
<input type="file" name="image" class="file_input_hidden site-image file_upload" onchange="javascript: document.getElementById('fileName').value = this.value;" />
<br />Al menos de 200 de ancho x 200 de alto

<!-- Post Validation of Content and Submit Button -->
<?php wp_nonce_field('post_nonce', 'post_nonce_field'); ?>
<input type="hidden" name="submitted" id="submitted" value="true" />
<button type="submit"><?php _e('Add Post', 'framework') ?></button>

</div><!-- #content -->
</div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>
  • Thank you so much for your reply, I tried to modify the code and add what you suggest, but I get Parse Error, thanks in advance for your help. Commented Oct 12, 2013 at 16:11

2 Answers 2


Your code doesn't assign the terms to the post. You can do this with wp_set_post_terms() which does:

Set terms for a post.

Simplified usage example:

$pet_cat_ term = $_POST['pet_category'];  
wp_set_post_terms( $post_id, $pet_cat_ term, 'pet-category' );

Additional note: use the id as value for your term select(s), to prevent problems, i.e. because for hierarchical terms you have to do it anyways.


Add term for custom taxonomy from front end through ajax


 <div class="articles_catform">
       <form id="articles_catform" name="articles_catform" class="wordpress-ajax-form2" method="post" action="<?php echo admin_url('admin-ajax.php'); ?>" enctype="multipart/form-data" >
          <input type="text" name="name" placeholder="Category Title">
           <textarea name="description" placeholder="Category Description" style="height: 200px !important;"></textarea> 

          <input type="hidden" name="action" value="custom_cataction">



  if( document.articles_catform.name.value == "") {
  alert( "Please provide Title! " );
  document.articles_catform.name.focus() ;
  return false;
  if(document.articles_catform.description.value == "" ) {
  alert( "Please provide your Description! ");
  document.articles_catform.description.focus() ;
  return false;


      var $form = $(this);
      $.post($form.attr('action'), $form.serialize(), function(data) {
        //return false;


          alert('Category Created Successfully...');
        else {
           alert('Already Exists!!!');



Add In function file

add_action('wp_ajax_custom_cataction', 'custom_cataction');
add_action('wp_ajax_nopriv_custom_cataction', 'custom_cataction');
function custom_cataction() {
    $post_name = (trim($_POST['name']));
    $post_description = (trim($_POST['description']));

    $cid = wp_insert_term($post_name, 'articles_category', array(
    'description' => $post_description,

    if ( is_wp_error($cid) ) {
     //echo $cid->get_error_message();

    else  {

  • Welcome to WPSE! You have posted several answers to old questions recently. Your effort is appreciated. However, if you only post code without any explanation how it works and how it solves OP's question, you will find it difficult to attract attention to your answers. You may study the answers of high ranking members to see which approach is awarded most by the community.
    – cjbj
    Commented Feb 14, 2020 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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