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