I’ve been trying to add a custom posts type to my wordpress page but its not showing up.
What am I doing wrong?
It feels like its only a small thing but I’m not seeing it as I me fairly new to the custom posts type’s.
What I want is to create pages for the teams at our company where we can showcase all our members
<?php
function mon_register_post_type() {
$labels = array(
'name' => __( 'Teams' ),
'singular_name' => __( 'Team' ),
'add_new' => __( 'Add new'),
'add_new_item' => __( 'Add New member'),
'edit_item' => __( 'Edit member'),
'new_item' => __( 'New member'),
'view_item' => __( 'View members'),
'search_items' => __( 'Search members'),
'not_found' => __( 'No memebers Found' ),
'not_found_in_trash' => __( 'No members found in Trash' )
);
$args = array(
'labels' => $labels,
'has_archive' => true,
'public' => true,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'show_in_rest' => true,
'supports' => array('title','editor', 'excerpt', 'custom-fields', 'thumbnail', 'page-attributes'),
'taxonomies' => array( 'teams' ),
'rewrite' => array( 'slug' => 'member' ),
);
register_post_type( 'mon_member', $args );
register_taxonomy('teams', ['team'], [
'label' => __('Teams', 'txtdomain'),
'hierarchical' => true,
'rewrite' => ['slug' => 'team'],
'show_admin_column' => true,
'show_in_rest' => true,
'labels' => [
'singular_name' => __('Team', 'txtdomain'),
'all_items' => __('All Teams', 'txtdomain'),
'edit_item' => __('Edit Team', 'txtdomain'),
'view_item' => __('View Team', 'txtdomain'),
'update_item' => __('Update Team', 'txtdomain'),
'add_new_item' => __('Add New Team', 'txtdomain'),
'new_item_name' => __('New Genre Team', 'txtdomain'),
'search_items' => __('Search Teams', 'txtdomain'),
'parent_item' => __('Parent Teams', 'txtdomain'),
'parent_item_colon' => __('Parent Team:', 'txtdomain'),
'not_found' => __('No Teams found', 'txtdomain'),
]
]);
register_taxonomy_for_object_type('teams', 'team');
}
add_action( 'init', 'mon_register_post_type' );
?>
Advertisement
Answer
The way i always do it is this way, register the custom post type as you already have done
register_post_type( 'magazines',
// CPT Options
array(
'labels' => array(
'name' => __( 'Magazines' ),
'singular_name' => __( 'magazines' ),
'archives' => __( 'magazines' ),
),
'public' => true,
'has_archive' => true,
'show_in_rest' => true,
'rewrite' => array('slug' => 'magazines'),
'show_in_nav_menu' => true,
'menu_position' => 9,
'supports' => array('title', 'editor', 'thumbnail', 'author'),
)
);
and then the taxonomy register like this
function magazine_type_taxonomy() {
$labels = array(
'name' => _x( 'Magazine Categories', 'magazine-taxonomy', 'text_domain' ),
'singular_name' => _x( 'Magazine Categories', 'magazine-taxonomy', 'text_domain' ),
'menu_name' => __( 'Magazine Categories', 'text_domain' ),
'all_items' => __( 'All Items', 'text_domain' ),
'parent_item' => __( 'Parent Item', 'text_domain' ),
'parent_item_colon' => __( 'Parent Item:', 'text_domain' ),
'new_item_name' => __( 'New Item Name', 'text_domain' ),
'add_new_item' => __( 'Add New Item', 'text_domain' ),
'edit_item' => __( 'Edit Item', 'text_domain' ),
'update_item' => __( 'Update Item', 'text_domain' ),
'view_item' => __( 'View Item', 'text_domain' ),
'add_or_remove_items' => __( 'Add or remove items', 'text_domain' ),
'choose_from_most_used' => __( 'Choose from the most used', 'text_domain' ),
'popular_items' => __( 'Popular Items', 'text_domain' ),
'search_items' => __( 'Search Items', 'text_domain' ),
'not_found' => __( 'Not Found', 'text_domain' ),
'no_terms' => __( 'No items', 'text_domain' ),
'items_list' => __( 'Items list', 'text_domain' ),
'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'show_in_rest' => true,
'rest_base' => 'mag_type',
'rest_controller_class' => 'WP_REST_Terms_Controller',
);
register_taxonomy( 'magazine-taxonomy', array( 'magazines', 'digital'), $args );
}
add_action( 'init', 'magazine_type_taxonomy', 0 );
Notice how i am assigning the custom post type in the taxonomy not in the actual post type itself