0

This issue seems to be quite common, but the solutions that worked for others haven't worked for me.

I'm using the CPT UI plugin and have created a CPT called 'courses' with the following settings:

register_post_type('courses', array(
'label' => 'Courses',
'description' => '',
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'capability_type' => 'post',
'map_meta_cap' => true,
'hierarchical' => true,
'rewrite' => array('slug' => 'courses', 'with_front' => true),
'query_var' => true,
'supports' => array('title','editor','excerpt','trackbacks','custom-fields','comments','revisions','thumbnail','author','page-attributes','post-formats'),
'labels' => array (
  'name' => 'Courses',
  'singular_name' => 'Course',
  'menu_name' => 'Courses',
  'add_new' => 'Add Course',
  'add_new_item' => 'Add New Course',
  'edit' => 'Edit',
  'edit_item' => 'Edit Course',
  'new_item' => 'New Course',
  'view' => 'View Course',
  'view_item' => 'View Course',
  'search_items' => 'Search Courses',
  'not_found' => 'No Courses Found',
  'not_found_in_trash' => 'No Courses Found in Trash',
  'parent' => 'Parent Course',
)

I then created a page called 'courses' and a specialized page template called 'page-courses.php' and a template called 'single-courses.php' for any posts within the courses custom post type. Up to now everything worked correctly.

Next, I created a new page with the slug of 'questions-checkout' and 'courses' as it's parent. It is this page that gives a 404.

I've tried the following to get it working:

  1. In admin: settings > permalinks and resaved added flush_rewrite_rules(); to functions.php and refreshed the 'questions-checkout' page
  2. Installed 'rewrite rules' plugin and flushed the rewrite rules
  3. Created a new custom post type using CPT UI.

Using one or more of the above 3 fixes does indeed fix the 404 on 'questions-checkout'. However, I then get a 404 on posts within the courses page e.g. /courses/post-title.

No matter how hard I try, it's one or the other that works - I can't get both courses posts and child pages to be 404-free.

Strictly speaking, I know that pages should not have the same name as a custom post type, so I suspect this is the cause of the 404. My next approach was to delete the 'courses' page, and its child 'questions-checkout'. Then I changed 'page-courses.php' to 'archive-courses.php' and set 'has_archive' to 'true'.

So as it stands, I have a working archive pages and singular pages for my 'courses' custom post type. But how can I now add a page called 'questions-checkout' with the URL /courses/questions-checkout ?

I'm using WordPress 3.9.1 multisite.

2 Answers 2

0

When you register a new post type for the first time, you need to visit Settings -> Permalinks so WordPress can set up the permalink rules for it. You don't even need to hit save, visiting the page is enough.

9
  • Hi Eric. I've tried this many times I'm afraid. I've even tried changing the name of the CPT to avoid possible name clashes and adding the custom rewrite slug of 'courses'. It seems when there is a custom post type with archive and single page templates AND a child page, a 404 will appear on one of the 3. Updating the permalink/rewrite rules just pushes the 404 from one to another.
    – Chris
    Jul 2, 2014 at 15:19
  • So /courses works, but /courses/page-one does not? Jul 2, 2014 at 15:25
  • The scenario is I have /courses, /courses/post-title and /courses/childpage. I get the 404 on /courses/childpage, but using the permalink/rewrite flushing methods, the 404 will jump around. So if I manage to get /courses/childpage working, /courses/post-title will show a 404.
    – Chris
    Jul 2, 2014 at 15:28
  • Interesting. The child page would actually be /courses/parent-page/child-page though, correct? If not, there's the problem right there. Jul 2, 2014 at 16:06
  • The way I'm currently doing it is I have a custom post type of 'kc_courses' to avoid conflicts with the word 'courses'. The post type has the custom rewrite slug of 'courses'. I have a parent page called 'courses' and a child page of courses called 'questions-checkout'. I also have a specialized page template for the courses page called page-courses.php and single-kc_courses.php for the custom post type single pages.
    – Chris
    Jul 2, 2014 at 20:31
0

With a lot of help from @Eric Holmes, I think we've found a solution.

The basic idea is to add my own rewrite rules to create the permalink structure I'm after.

I added this to my functions.php:

function add_rewrite_rules(){
   global $wp;
   $wp->add_query_var( 'fakepage' );
   $parentpage = 'ADD THE TOP LEVEL PAGE'S POST ID HERE';
   add_rewrite_rule('courses/checkout/([^/]*)','index.php?page_id='.$parentpage.'&fakepage=$matches[1]','top');
}
add_action('init','add_rewrite_rules');

Then in page-pagename.php (in my case, page-courses.php):

if(isset($wp_query->query_vars['fakepage'])){
   $fakepage = $wp_query->query_vars['fakepage'];
   if($fakepage=='questions'){
      //do whatever
   } else {
      //do whatever
   }
}

Your Answer

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

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