Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Register WordPress custom post type and tax, where single items view is redirected to 404 page but item archives are still accessible.
<?php
// post type
function cpt_items_register() {
$cpt_labels = array(
'name' => _x( 'Items', 'Post Type General Name', '{domain}' ),
'singular_name' => _x( 'Item', 'Post Type Singular Name', '{domain}' ),
'menu_name' => esc_html__( 'Items', '{domain}' ),
'name_admin_bar' => esc_html__( 'Page Block', '{domain}' ),
'archives' => esc_html__( 'Item Archives', '{domain}' ),
'attributes' => esc_html__( 'Item Attributes', '{domain}' ),
'parent_item_colon' => esc_html__( 'Parent Item:', '{domain}' ),
'all_items' => esc_html__( 'All Items', '{domain}' ),
'add_new_item' => esc_html__( 'Add New Item', '{domain}' ),
'add_new' => esc_html__( 'Add New Item', '{domain}' ),
'new_item' => esc_html__( 'New Item', '{domain}' ),
'edit_item' => esc_html__( 'Edit Item', '{domain}' ),
'update_item' => esc_html__( 'Update Item', '{domain}' ),
'view_item' => esc_html__( 'View Item', '{domain}' ),
'view_items' => esc_html__( 'View Items', '{domain}' ),
'search_items' => esc_html__( 'Search Items', '{domain}' ),
'not_found' => esc_html__( 'Not found', '{domain}' ),
'not_found_in_trash' => esc_html__( 'Not found in Trash', '{domain}' ),
'items_list' => esc_html__( 'Blocks list', '{domain}' ),
'items_list_navigation' => esc_html__( 'Blocks list navigation', '{domain}' ),
'filter_items_list' => esc_html__( 'Filter items list', '{domain}' ),
'featured_image' => esc_html__( 'Featured Image', '{domain}' ),
'set_featured_image' => esc_html__( 'Set featured image', '{domain}' ),
'remove_featured_image' => esc_html__( 'Remove featured image', '{domain}' ),
'use_featured_image' => esc_html__( 'Use as featured image', '{domain}' ),
);
$cpt_args = array(
'label' => esc_html__( 'Item', '{domain}' ),
'description' => esc_html__( 'Item description', '{domain}' ),
'labels' => $cpt_labels,
'supports' => array( 'title','editor','excerpt','revisions', 'author','thumbnail'),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_admin_bar' => true,
'show_in_nav_menus' => false,
'has_archive' => 'items',
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'query_var' => false // disables item single view and redirects it to 404 page
);
register_post_type( 'item', $cpt_args );
}
add_action( 'init', 'cpt_items_register', 0 );
// tax
function cpt_items_tax_register() {
$ctx_labels = array(
'name' => _x( 'Items Categories','taxonomy general name', '{domain}' ),
'singular_name' => _x( 'Item Category','taxonomy singular name', '{domain}' ),
'search_items' => esc_html__( 'Search Categories', '{domain}' ),
'popular_items' => esc_html__( 'Popular Categories', '{domain}' ),
'all_items' => esc_html__( 'All Categories', '{domain}' ),
'parent_item' => esc_html__( 'Parent Category', '{domain}' ),
'edit_item' => esc_html__( 'Edit Category', '{domain}' ),
'update_item' => esc_html__( 'Update Category', '{domain}' ),
'add_new_item' => esc_html__( 'Add New Category', '{domain}' ),
'new_item_name' => esc_html__( 'New Category', '{domain}' ),
'separate_items_with_commas' => esc_html__( 'Separate categories with commas', '{domain}' ),
'add_or_remove_items' => esc_html__( 'Add or remove categories', '{domain}' ),
'choose_from_most_used' => esc_html__( 'Choose from most used categories', '{domain}' )
);
$rewrite = array(
'slug' => 'items',
'hierarchical' => true,
'with_front' => false
);
$ctx_args = array(
'label' => esc_html__('Items Categories', '{domain}' ),
'labels' => $ctx_labels,
'public' => true,
'hierarchical' => true,
'show_ui' => true,
'show_in_nav_menus' => true,
'show_admin_column' => true,
'rewrite' => $rewrite,
);
register_taxonomy( 'items', 'item', $ctx_args );
}
add_action('init', 'cpt_items_tax_register');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.