I am creating custom post types using this code

function create_custom_post_type() {
            'labels' => array(
                'name' => __('Courses'),
                'singular_name' => __('Course')
            'public' => true,
            'has_archive' => true,
            'rewrite' => array('slug' => 'courses'),
            'supports' => array('title', 'editor', 'thumbnail', 'excerpt'),
            'taxonomies' => array('category')
add_action('init', 'create_custom_post_type');

This code successfully works and shows and the archive pages shows as well but the posts give a 404 error, If i name the custom post types anything else like Lessons then it works I have done everything like cleared the rewrite rules, saved the permalink settings again after creating a post but it does not work Can anyone help Thanks,

  • courses is quite generic are you sure there isn't already a CPT with the same slug? I also saw you put 'rewrite' => array('slug' => 'courses'), in the code but this seems unnecessary given the CPT already has the name courses and is probably causing problems. Have you used a rewrite rules tester plugin to confirm it matches the rules from the CPT and not something 3rd party/something else it shouldn't?
    – Tom J Nowell
    Commented Nov 7, 2023 at 14:44
  • @TomJNowell i did remove the slug now but it's still not working, can you recommend me some rewrite tester plugins, what i am not understanding is that why are other CPT working but not with this name, i have not created any other CPT as well
    – Waleed
    Commented Nov 7, 2023 at 18:03
  • @TomJNowell ok i did test using a tester plugin and i see another rewrite rule for course, could this be conflicting with the name courses?
    – Waleed
    Commented Nov 7, 2023 at 18:43
  • yes, WP will go from the highest priority rule and use the first one that matches then ignore the rest, if your rule matches too but has a lower priority it will not be used or even tested
    – Tom J Nowell
    Commented Nov 7, 2023 at 19:06
  • @TomJNowell so is there no way i can use courses word for custom post type even though the other rewrite rule is named 'course' not 'courses'
    – Waleed
    Commented Nov 7, 2023 at 20:21


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.