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(
'title',
'editor'
)
);
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(
$_POST['category']
)
)
);
$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 ?
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 iftaxonomy_exists()
is returningfalse
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 beforeregister_taxonomy
has had a chance to run. Make sure that your form-processing function is hooked to something later thaninit:10
.You shouldn’t need to use:
and the
tax_input
array value. Thelatterformer (Please see end remark) 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 usingarray($_POST['category'])
will be causing the problems. Try:Remark
Rarst makes a good point in the comments which I think needs highlighting:
I found using
isset()
allowed me to usewp_insert_post()
on custom post types: