I'm getting a 404 on my created custom-post-type when visiting the post URL.
I changed the custom set slug from the built-in WP category that I'm using to default blank setting which results in success in seeing the single-{post-type}.php post but with the drawback that my pretty permalink structure isn't as I want it to be.
Custom post type: "news" which uses the built-in WP "Category"
/*----------------------------*/
/*-- Custom post type: News --*/
/*----------------------------*/
function dm_cptNews() {
$labels = array(
'name' => _x( 'News items', 'Post Type General Name', 'foobar-2k19' ),
'singular_name' => _x( 'News Item', 'Post Type Singular Name', 'foobar-2k19' ),
'menu_name' => __( 'News items', 'foobar-2k19' ),
'name_admin_bar' => __( 'News Item', 'foobar-2k19' ),
'archives' => __( 'News items Archives', 'foobar-2k19' ),
'attributes' => __( 'News Item Attributes', 'foobar-2k19' ),
'parent_item_colon' => __( 'Parent News Item:', 'foobar-2k19' ),
'all_items' => __( 'All News Items', 'foobar-2k19' ),
'add_new_item' => __( 'Add New News Item', 'foobar-2k19' ),
'add_new' => __( 'Add New News Item', 'foobar-2k19' ),
'new_item' => __( 'New News Item', 'foobar-2k19' ),
'edit_item' => __( 'Edit News Item', 'foobar-2k19' ),
'update_item' => __( 'Update News Item', 'foobar-2k19' ),
'view_item' => __( 'View News Item', 'foobar-2k19' ),
'view_items' => __( 'View News Items', 'foobar-2k19' ),
'search_items' => __( 'Search News Item', 'foobar-2k19' ),
'not_found' => __( 'Not found', 'foobar-2k19' ),
'not_found_in_trash' => __( 'Not found in Trash', 'foobar-2k19' ),
'featured_image' => __( 'Featured Image', 'foobar-2k19' ),
'set_featured_image' => __( 'Set featured image', 'foobar-2k19' ),
'remove_featured_image' => __( 'Remove featured image', 'foobar-2k19' ),
'use_featured_image' => __( 'Use as featured image', 'foobar-2k19' ),
'insert_into_item' => __( 'Insert into news item', 'foobar-2k19' ),
'uploaded_to_this_item' => __( 'Uploaded to this news item', 'foobar-2k19' ),
'items_list' => __( 'News items list', 'foobar-2k19' ),
'items_list_navigation' => __( 'News Items list navigation', 'foobar-2k19' ),
'filter_items_list' => __( 'Filter news items list', 'foobar-2k19' ),
);
$args = array(
'label' => __( 'News Item', 'foobar-2k19' ),
'description' => __( 'News custom post type.', 'foobar-2k19' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'revisions', 'post-formats', 'excerpt' ),
'taxonomies' => array( 'category' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 27,
'menu_icon' => 'dashicons-megaphone',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => false,
'rewrite' => array('slug' => 'news/%category%'),
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
);
register_post_type( 'news', $args );
}
add_action( 'init', 'dm_cptNews', 0 );
Fill %category% for prettypermalinks
/*-------------------------------*/
/*-- Custom slug News category --*/
/*-------------------------------*/
function dm_customSlugNewsCategory( $post_link, $id = 0 ){
$post = get_post($id);
if ( is_object( $post ) ){
$terms = wp_get_object_terms( $post->ID, 'category' );
if( $terms ){
return str_replace( '%category%' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'dm_customSlugNewsCategory', 1, 3 );
Permalink setup
I'm aware that WordPress "hates" conflicting slugs, but from an SEO perspective it has to be doable. I don't want to fix the issue with some third-party plugins either.
In short: The WP built-in "Category" slug should be changed into "news" and should be able to display the single-news.php
(and yes, I flushed the permalinks after every edit.)