0

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.org/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 );
}
2
  • when you getting 404 error at add new time or view post time? – Jayesh May 31 '19 at 9:30
  • At view post time – CatK May 31 '19 at 13:40
0

Your 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.

1
  • Hi, I've already tried this several times (as stated in my post) – CatK May 31 '19 at 13:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.