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

if($post_id)
{

// 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 -->
<fieldset>
<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" />
</fieldset>

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

<!-- Post Content -->
<fieldset>
<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>
</fieldset>

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

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

<!-- 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
</fieldset> 

<!-- Post Validation of Content and Submit Button -->
<fieldset>
<?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>
</fieldset>
</form>

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

<?php get_sidebar(); ?>
<?php get_footer(); ?>
share|improve this question
    
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. –  nestorgallego Oct 12 '13 at 16:11
    
I made it, thank you so much! –  nestorgallego Oct 12 '13 at 16:29

1 Answer 1

up vote 0 down vote accepted

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.

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.