Skip to main content

wp_insert_post custom taxonomy

I have a custom post type "books" and a custom taxonomy books-categories. wp_insert_post() works but when i choose a category and submit the form the post displays ut the category isn't selected. Can anyone help me please?

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 {
    echo 'Please enter a  title';
}
if (isset ($_POST['description'])) {
    $description = $_POST['description'];
} else {
    echo 'Please enter the content';
}
$tags = $_POST['post_tags'];

// Add the content of the form to $post as an array
$new_post = array(
    'post_title'    => $title,
    'post_content'  => $description,
    'post_category' => array($_POST['cat']),  // Usable for custom taxonomies too
    'tags_input'    => array($tags),
    'post_status'   => 'draft',           // Choose: publish, preview, future, draft, etc.
    'post_type' => 'books'                //'post',page' or use a custom post type if you want to
);
//save the new post
$pid = wp_insert_post($new_post); 
//insert taxonomies      

}

Billy
  • 57
  • 3
  • 13