You should hook into save_post
and set the categories using wp_set_object_terms()
:
// Add an action to run on post save
add_action( 'save_post', 'set_genre_on_save' );
function set_genre_on_save( $post_id ){
// Check the post type
if (is_single('tvseries')) {
// Get the custom field data
$custom_field_data = get_post_custom( $post_id );
// Check if there is any genres entered in the metabox
if (isset($custom_box_data['genre'])) {
// Save the genre data (separated by comma) into an array
$genre_array = explode( ',', $custom_box_data['genre'] );
//Set the array values to lower case
foreach ($genre_array as $genre){
$genre = strtolower($genre);
}
// Set the categories for these genres
wp_set_object_terms( $post_id, $genre_array, 'category' );
}
}
}
You should enter the slug
or the ID
of your categories in the field. for example, War Movies
won't work, but war-movies
will work. Also there should be no white space between the values (or you have to change ','
to ' ,'
).
Note that this is just an example, since you didn't post any code. You might need to change some of the values such as genres
to match your custom fields / post types.
Tvseries
directly. So why do you need the custom fieldGenres
for?