You can do this with custom hierarchical taxonomy and use it for your custom post type.
Register the Taxonomy area
for the post type content
using the init
action hook:
function wporg_register_taxonomy_area() {
$labels = array(
'name' => _x( 'Areas', 'taxonomy general name' ),
'singular_name' => _x( 'Area', 'taxonomy singular name' ),
'search_items' => __( 'Search Areas' ),
'all_items' => __( 'All Areas' ),
'parent_item' => __( 'Parent Area' ),
'parent_item_colon' => __( 'Parent Area:' ),
'edit_item' => __( 'Edit Area' ),
'update_item' => __( 'Update Area' ),
'add_new_item' => __( 'Add New Area' ),
'new_item_name' => __( 'New Area Name' ),
'menu_name' => __( 'Area' ),
);
$args = array(
'hierarchical' => true, // make it hierarchical (like categories)
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => [ 'slug' => 'area' ],
);
register_taxonomy( 'area', [ 'content' ], $args );
}
add_action( 'init', 'wporg_register_taxonomy_area' );
Register custom post type content
using the init
action hook:
function my_custom_content_item() {
$labels = array(
'name' => _x( 'Content', 'post type general name' ),
'singular_name' => _x( 'Content', 'post type singular name' ),
'menu_name' => 'Content'
);
$args = array(
'labels' => $labels,
'public' => true,
'has_archive' => true,
'taxonomies' => array( 'area' ),
'show_in_rest' => true,
);
register_post_type( 'content', $args );
}
add_action( 'init', 'my_custom_content_item' );
Adds the registered taxonomy area
to object type content
.
function attach_area_to_content() {
register_taxonomy_for_object_type( 'area', 'content' );
}
add_action('init','attach_area_to_content');