I am trying to create a meta box (with the same feature as the category meta box, the one that searches categories) , except I want to make one that searches a custom category.
I’m hoping that I can rely on a predefined callback function that will work with custom categories.
I tried the method from this article but to no avail:
add_meta_box(
'metaboxID',
"Meta box Title",
'myCustomPostType_myCustomCategory_meta_box',
'myCustomPostType',
'side',
'high'
);
When using add_meta_box
, is there a predefined callback function for custom categories, such as post_categories_meta_box
?
1 Answer
The callback you’re looking for is post_categories_meta_box
which we can use via a custom callback.
// Add your custom meta box.
function your_custom_meta_box() {
add_meta_box( 'your-custom-meta-box', 'Custom Meta Box', 'my_taxonomy_meta_box_cb', 'post', 'side', 'high', null );
}
add_action( 'add_meta_boxes', 'your_custom_meta_box' );
// Our custom callback.
function my_taxonomy_meta_box_cb( $post, $box ) {
// Pass in the taxonomy we'd like to use.
$box[ 'args' ][ 'taxonomy' ] = 'listing_category';
post_categories_meta_box( $post, $box );
}
Thanks to @birgire for pointing out how not to duplicate code. 🙂