I have a custom post type called "nutrix-recipe".
It has a lot of taxonomies, for example "nutrix-category" or "nutrix-tag".
In post_edit.php, I removed all taxonomy meta boxes and added select2 fields to allow selection of terms. This works so far. The meta is saved.
This is, for example, how nurix-category is displayed inside the display_metbox function:
// CATEGORIES FIELD
$html = '';
$label = __( 'Category: ', 'nutrix' );
$current_tags = get_post_meta( $post_object->ID, 'nutrix_select2_categories',true );
if( $categories = get_terms( 'nutrix-category', 'hide_empty=0' ) ) {
$html .= '<div class="nutrix-div-20"><p><label for="nutrix_select2_categories">' . $label . '</label><br /><select id="nutrix_select2_categories" name="nutrix_select2_categories[]" >';
foreach( $categories as $category ) {
$selected = ( is_array( $current_tags ) && in_array( $category->term_id, $current_tags ) ) ? ' selected="selected"' : '';
$html .= '<option value="' . $category->term_id . '"' . $selected . '>' . $category->name . '</option>';
}
$html .= '</select></p></div>';
echo $html;
}
This is the nutrix-tag display:
// TAGS FIELD
$html = '';
$label = __( 'Tags: ', 'nutrix' );
$current_tags = get_post_meta( $post_object->ID, 'nutrix_select2_tags',true );
if( $tags = get_terms( 'nutrix-tag', 'hide_empty=0' ) ) {
$html .= '<div class="nutrix-div-40"><p><label for="nutrix_select2_tags">' . $label . '</label><br /><select id="nutrix_select2_tags" name="nutrix_select2_tags[]" multiple="multiple" >';
foreach( $tags as $tag ) {
$selected = ( is_array( $current_tags ) && in_array( $tag->term_id, $current_tags ) ) ? ' selected="selected"' : '';
$html .= '<option value="' . $tag->term_id . '"' . $selected . '>' . $tag->name . '</option>';
}
$html .= '</select></p></div>';
echo $html;
}
I save them this way (nonce is missing atm):
function nutrix_save_recipe_mb( $post_id, $post ) {
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return $post_id;
// if post type is different from our selected one, do nothing
if ( $post->post_type == 'nutrix-recipe' ) {
if( isset( $_POST['nutrix_select2_categories'] ) )
update_post_meta( $post_id, 'nutrix_select2_categories', $_POST['nutrix_select2_categories'] );
else
delete_post_meta( $post_id, 'nutrix_select2_categories' );
if( isset( $_POST['nutrix_select2_tags'] ) )
update_post_meta( $post_id, 'nutrix_select2_tags', $_POST['nutrix_select2_tags'] );
else
delete_post_meta( $post_id, 'nutrix_select2_tags' );
}
}
All these work. But my problem is, that the taxonomy terms are not assigned to the post. Or from another view: If I visit the edit tags page, the category count remains zero.
Is it possible to not only save the meta, but also set the taxonomies ?
Thank you :)