The post_categories_meta_box() WordPress PHP function displays post categories form fields.
Usage
post_categories_meta_box( $post, $box );
Parameters
$post(WP_Post) (required) – Current post object.$box(array) (required) – Categories meta box arguments.id(string) – Meta box ‘id’ attribute.title(string) – Meta box title.callback(callable) – Meta box display callback.args(array) – Extra meta box arguments.taxonomy(string) – Taxonomy. Default ‘category’.
More information
See WordPress Developer Resources: post_categories_meta_box
Examples
Display the categories meta box in a custom post type
In this example, we will add the categories meta box to a custom post type called ‘product’.
// Register the custom post type
function create_product_post_type() {
register_post_type('product',
array(
'labels' => array(
'name' => __('Products'),
'singular_name' => __('Product')
),
'public' => true,
'has_archive' => true,
'supports' => array('title', 'editor', 'thumbnail', 'custom-fields', 'revisions', 'categories')
)
);
}
add_action('init', 'create_product_post_type');
// Add the categories meta box to the 'product' post type
function add_categories_meta_box_to_product() {
add_meta_box('categorydiv', __('Categories'), 'post_categories_meta_box', 'product', 'side', 'default', array('taxonomy' => 'category'));
}
add_action('add_meta_boxes', 'add_categories_meta_box_to_product');
Change the title of the categories meta box
In this example, we change the title of the categories meta box to “Topics”.
function change_categories_meta_box_title() {
remove_meta_box('categorydiv', 'post', 'side');
add_meta_box('categorydiv', __('Topics'), 'post_categories_meta_box', 'post', 'side', 'default', array('taxonomy' => 'category'));
}
add_action('add_meta_boxes', 'change_categories_meta_box_title');
Display a custom taxonomy in the meta box
In this example, we display a custom taxonomy named “genres” in the meta box.
function add_genres_meta_box_to_post() {
add_meta_box('genresdiv', __('Genres'), 'post_categories_meta_box', 'post', 'side', 'default', array('taxonomy' => 'genres'));
}
add_action('add_meta_boxes', 'add_genres_meta_box_to_post');
Add the categories meta box to a custom meta box
In this example, we add the categories meta box to a custom meta box named “custom_meta_box”.
function add_custom_meta_box() {
add_meta_box('custom_meta_box', __('Custom Meta Box'), 'custom_meta_box_callback', 'post', 'side', 'default');
}
add_action('add_meta_boxes', 'add_custom_meta_box');
function custom_meta_box_callback($post) {
post_categories_meta_box($post, array('taxonomy' => 'category'));
}
Display multiple taxonomies in the meta box
In this example, we display multiple taxonomies, “category” and “genres”, in the meta box.
function add_multiple_taxonomies_meta_box_to_post() {