0

This side of WordPress is completely new to me, but is also extremely important. I have created a form that allows me to upload a page, with all the parameters I need, however a custom taxonomy is not assigned to the page. However, I need a drop down of all the terms in my custom taxonomy 'walls', so that the user can pick the term and it is assigned to the post. I have googled and searched on stack exchange and yet none of the code examples I found have worked out for me. I have tried the code with categories and it works fine.

Code:

<?php if ( is_user_logged_in() ) { ?>

<?php
$postTitleError = '';

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

if(trim($_POST['postTitle']) === '') {
$postTitleError = 'Please enter a title.';
$hasError = true;
} else {
$postTitle = trim($_POST['postTitle']);
}

$post_information = array(
'post_title' => esc_attr(strip_tags($_POST['postTitle'])),
'post_content' => esc_attr(strip_tags($_POST['postContent'])),
'post_type' => 'page',
'page_template'  => 'Review.php',
'post_status' => 'pending',
'tax_input' => array( 'walls' => array( $_POST['pickWall'] ) ),
);

$post_id = wp_insert_post($post_information);

if($post_id)
{
// Update Custom Meta
update_post_meta($post_id, 'product_aesthetics', esc_attr(strip_tags($_POST['product_aesthetics_meta'])));

wp_redirect(home_url());
exit;
}

}

?>

<?php get_header(); ?>


<!-- #primary BEGIN -->
<div id="primary">

<form action="" id="primaryPostForm" method="POST">

<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 } ?>

<fieldset>

<label for="postContent"><?php _e('Post\'s Content:', 'framework') ?></label>

<textarea name="postContent" id="postContent" rows="8" cols="30"><?php if(isset($_POST['postContent'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['postContent']); } else { echo $_POST['postContent']; } } ?></textarea>

</fieldset>


<fieldset>

<label for="product_aesthetics_meta"><?php _e('Product Aesthetics Meta:', 'framework') ?></label>

<textarea name="product_aesthetics_meta" id="product_aesthetics_meta" rows="8" cols="30"><?php if(isset($_POST['product_aesthetics_meta'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['product_aesthetics_meta']); } else { echo $_POST['product_aesthetics_meta']; } } ?></textarea>

</fieldset>

<fieldset>
<label for="pickWall">Type of wall</label> 
<select name="pickWall">

<?php

// ======= Custom post types category drop down ======== 
$taxonomy = 'walls';
$terms = get_terms($taxonomy); // Get all terms of a taxonomy

if ( $terms && !is_wp_error( $terms ) ) :

foreach ( $terms as $term ) { 
echo '<option value="' . get_term_link($term->slug, $taxonomy) . '">' . $term->name . '</option>';
}

endif;
?>

</select>
</fieldset>


<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><!-- #primary END -->

<?php } ?>

1 Answer 1

0

First, you should change your input field names to something unique. category is a WordPress query var, so submitting a form with that field name may have unexpected results.

That said, post_category is only for the category taxonomy, custom taxonomies should use the tax_input parameter.

however... if this form is being submitted by users who are not logged in or don't have the capability to assign terms in your custom taxonomy, then you must use wp_set_object_terms to assign terms after the post is inserted. See the note in wp_insert_post:

'tax_input': Equivalent to calling wp_set_post_terms() for each custom taxonomy in the array. If the current user doesn't have the capability to work a taxonomy, the you must use wp_set_object_terms() instead.

EDIT-

for the select field, you can output the term ID as the option value:

<?php

// ======= Custom post types category drop down ======== 
$taxonomy = 'walls';
$terms = get_terms($taxonomy); // Get all terms of a taxonomy

if ( $terms && !is_wp_error( $terms ) ) :

foreach ( $terms as $term ) { 
    echo '<option value="' . $term->term_id . '">' . $term->name . '</option>';
}

endif;
?>

Then when you insert the post:

'tax_input' => array( 'walls' => array( $_POST['your_tax_field'] ) )
9
  • thanks for the reply, I have made the change to the input field names, thanks for the suggestion. Also, I am aware of the tax_input parameter, however I am not sure how to set the term in array( 'taxonomy' => array('term')) to the term chosen from the <select> taxonomy drop down. Finally, in terms of wp_set_object_terms the user will be logged in and will be able to assign the terms.
    – NDog
    Jan 16, 2015 at 18:02
  • see if the edit above answers your question.
    – Milo
    Jan 16, 2015 at 18:19
  • that looks like it should work! I have tried to do that, however it didn't work. Could you clarify for me where this value your_tax_field is from. I think I am writing the the wrong value. Thanks!
    – NDog
    Jan 16, 2015 at 18:21
  • your_tax_field is whatever you named your select field
    – Milo
    Jan 16, 2015 at 18:58
  • thank you very much for the reply. I have tried this and it does not work!
    – NDog
    Jan 16, 2015 at 19:20

Your Answer

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

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