I've declared a custom post type as below, I can create posts in admin but when I try to view I'm getting 404 error. I've declared a previous custom post type using similar code and it worked fine. I don't want an archive page, but do want single post pages.
I've tried flushing the rewrite rules, and deactivating plugins but neither has worked. Any help would be greatly appreciated!!
//create courses post type
add_action( 'init', 'create_courses_post_type' );
function create_courses_post_type() {
$labels = array(
'name' => __( 'Courses' ),
'singular_name' => __( 'Course' ),
'all_items' => __( 'All Courses' ),
'add_new' => _x( 'Add New Course', 'Courses' ),
'add_new_item' => __( 'Add New Course' ),
'edit_item' => __( 'Edit Course' ),
'new_item' => __( 'New Course' ),
'view_item' => __( 'View Course' ),
'search_items' => __( 'Search in Courses' ),
'not_found' => __( 'No Courses found' ),
'not_found_in_trash' => __( 'No Courses found in trash' ),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'has_archive' => false, // Set to false hides Archive Pages
'menu_icon' => 'dashicons-welcome-learn-more', //pick one here ~> /
'rewrite' => array( 'slug' => 'courses' ),
'taxonomies' => array( 'category', 'post_tag' ),
'query_var' => true,
'menu_position' => 9,
'publicly_queryable' => true
);
register_post_type( 'course', $args );
}
I've declared a custom post type as below, I can create posts in admin but when I try to view I'm getting 404 error. I've declared a previous custom post type using similar code and it worked fine. I don't want an archive page, but do want single post pages.
I've tried flushing the rewrite rules, and deactivating plugins but neither has worked. Any help would be greatly appreciated!!
//create courses post type
add_action( 'init', 'create_courses_post_type' );
function create_courses_post_type() {
$labels = array(
'name' => __( 'Courses' ),
'singular_name' => __( 'Course' ),
'all_items' => __( 'All Courses' ),
'add_new' => _x( 'Add New Course', 'Courses' ),
'add_new_item' => __( 'Add New Course' ),
'edit_item' => __( 'Edit Course' ),
'new_item' => __( 'New Course' ),
'view_item' => __( 'View Course' ),
'search_items' => __( 'Search in Courses' ),
'not_found' => __( 'No Courses found' ),
'not_found_in_trash' => __( 'No Courses found in trash' ),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'has_archive' => false, // Set to false hides Archive Pages
'menu_icon' => 'dashicons-welcome-learn-more', //pick one here ~> https://developer.wordpress/resource/dashicons/
'rewrite' => array( 'slug' => 'courses' ),
'taxonomies' => array( 'category', 'post_tag' ),
'query_var' => true,
'menu_position' => 9,
'publicly_queryable' => true
);
register_post_type( 'course', $args );
}
Share
Improve this question
asked May 31, 2019 at 8:18
CatKCatK
1
2
- when you getting 404 error at add new time or view post time? – Jayesh Commented May 31, 2019 at 9:30
- At view post time – CatK Commented May 31, 2019 at 13:40
1 Answer
Reset to default 0Your code is working perfectly. You have to refresh/flush permalinks after creating "Custom Post Type" hook.
Step 1: In the main menu find "Settings > Permalinks".
Step 2: Scroll down if needed and click "Save Changes".
Step 3: Rewrite rules and permalinks are flushed.