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 have create a custom post type in my WordPress attached with a custom taxonomy.

The post collect date from the from end of the web site by using wp_insert_post().

This is my Custom post registration code:

$labels = array(
        'name' => 'Films',
        'singular_name' => 'Film',
        'add_new' => 'Add new',
        'add_new_item' => 'Add new film',
        'edit_item' => 'Edit film',
        'new_item' => 'New film',
        'all_items' => 'All films',
        'view_item' => 'View film',
        'search_items' => 'Search films',
        'not_found' =>  'No films found',
        'not_found_in_trash' => 'No films found in Trash', 
        'parent_item_colon' => '',
        'menu_name' => 'Films'

    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true, 
        'show_in_menu' => true, 
        'query_var' => true,
        'rewrite' => true,
        'capability_type' => 'post',
        'has_archive' => true, 
        'hierarchical' => false,
        'menu_position' => null,
        'taxonomies' => array('film_category'),
        'supports' => array(

    register_post_type('film', $args);

And this is my Custom Taxonomy registration Code:

$labels = array(
        'name' => 'Category',
        'singular_name' => 'Film',
        'search_items' =>  'Search Categories',
        'all_items' => 'All Categories',
        'parent_item' => 'Parent Category',
        'parent_item_colon' => 'Parent Category:',
        'edit_item' => 'Edit Category', 
        'update_item' => 'Update Category',
        'add_new_item' => 'Add New Category',
        'new_item_name' => 'New Category Name',
        'menu_name' => 'Category'

    $args = array(
        'labels' => $labels,
        'hierarchical' => true,
        'rewrite' => false,
        'capabilities' => array('manage_terms') 

    register_taxonomy('film_category', array('film'), $args);

both are fired on init action.

And now when the end User submit the form from the front end I execute that code:

$post = array(
            'comment_status' => 'closed',
            'ping_status' => 'closed',
            'post_author' => 2,
            'post_content' => $_POST['synopsis'],
            'post_name' => sanitize_title($_POST['title']),
            'post_status' => 'private',
            'post_title' => $_POST['title'],
            'post_type' => 'film',
            'tax_input' => array(
                'film_category' => array(

        $new_film_id = wp_insert_post($post);

        wp_set_object_terms($new_film_id, $_POST['category'], 'film_category', true);

In my front end form I have the "category" select field that contain the film categories. I have try to add on options values the terms ID, the terms Name and the terms Slug but it doesn't work.

the new post is generated in my custom post type, but I can't assign the film categories.

Any idea please ?

share|improve this question
Had you checked that $_POST['category'] contains sane data at that point? Had you tried your code in dry run with test data, without front-end part? –  Rarst Mar 16 '12 at 11:53
I check my data with print_r a step after the assignment and there is the correct data. –  Merianos Nikos Mar 16 '12 at 11:57

3 Answers 3

up vote 2 down vote accepted

Where is the code that catches an processes the $_POST data? Is it in a template file? Or is it in a function that is run on a hook? If it's the latter, and if taxonomy_exists() is returning false as you suggest here (http://wordpress.stackexchange.com/questions/45798/wp-insert-post-and-custom-taxonomy#comment58402_45810), it's possible that you're checking before register_taxonomy has had a chance to run. Make sure that your form-processing function is hooked to something later than init:10.

share|improve this answer
Boone Gorges !!!! You are just perfect !!!!!! That worked for me. Until now I called the function that save my data imediatly, and now I hooked that function on init hook and worked !! :) Thanks a lot :) –  Merianos Nikos Mar 16 '12 at 14:06

You shouldn't need to use:

wp_set_object_terms($new_film_id, $_POST['category'], 'film_category', true);

and the tax_input array value. The latter should be sufficient.

If $_POST['category'] contains the term IDs, you will need to cast them as integers so they are not interpreted as slugs.

Also if $_POST['category'] is an array then using array($_POST['category']) will be causing the problems. Try:

 //Cas as array
 $terms = isset($_POST['category']) ? (array) $_POST['category'] : array();

 //Cast array values as integers if $_POST['category'] contains IDs
 $terms = array_map('intval',$terms);

 $post = array(
      //other arguments

      'tax_input' => array(
            'film_category' => $terms,

    $new_film_id = wp_insert_post($post);
share|improve this answer
Hello Stephen Harris. Thanks a lot for your assistance, but unfortunatly still not working. I don't realy know what's wrong. I have remove the wp_set_object_terms, and I use only your way. Any other idea :? Kind regards, Merianos Nikos –  Merianos Nikos Mar 16 '12 at 13:26
Note that wp_insert_post() will check for permission for tax_input and fail if user is not logged in. It's bad choice for non-typical post inserts, explicit wp_set_object_terms() is better for use cases like migrations, front-end submissions, etc. –  Rarst Mar 16 '12 at 13:33
After a deep search, I notice that the taxonomy looks like not exists. I have print out the $wp_taxonomies and the taxonomy exists into that global varialbe. But the taxonomy_exists return false :( –  Merianos Nikos Mar 16 '12 at 13:51

I found using isset() allowed me to use wp_insert_post() on custom post types:

if ( !isset( $id ) ) {
    $id = wp_insert_post( $new, true ); 
share|improve this answer
Please explain why this answers the question. –  kaiser Mar 11 at 0:05

Your Answer


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.