Ask your WordPress questions! Pay money and get answers fast! (more info)

404 when page sits inside custom post type slug

  • SOLVED

I have a people content type defined as below:




add_action( 'init', 'create_people' );
function create_people() {
$labels = array(
'name' => _x('People', 'post type general name'),
'singular_name' => _x('Person', 'post type singular name'),
'add_new' => _x('Add new person', 'Person'),
'add_new_item' => __('Add person'),
'edit_item' => __('Edit person'),
'new_item' => __('New person'),
'view_item' => __('View people'),
'search_items' => __('Search people'),
'not_found' => __('No people found'),
'not_found_in_trash' => __('No people found in Trash'),
'parent_item_colon' => ''
);

$supports = array('title');

register_taxonomy(
'position',
'person',
array(
'label' => __( 'Position' ),
'hierarchical' => true
)
);


register_post_type( 'person',
array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'supports' => $supports,
'rewrite' => array('slug' => 'people', 'with_front' => false),
'query_var' => true,
'menu_icon' => 'dashicons-admin-users'
)
);
}




This produces URLS like this website.com/people/person-name

I have a people landing page that exists at website.com/people/

My problem is that when I add a page as a child of the /people/ page, I get a 404 error

eg the page website.com/people/board/ produces a 404

yet the custom post type website.com/people/person-name works correctly as does website.com/people/

Any ideas on how to fix this?





Answers (6)

2016-03-04

Reigel Gallarde answers:

use this code...

add_action( 'init', 'reigel_init' );
function reigel_init() {
$GLOBALS['wp_rewrite']->use_verbose_page_rules = true;
}

add_filter( 'page_rewrite_rules', 'reigel_collect_page_rewrite_rules' );
function reigel_collect_page_rewrite_rules( $page_rewrite_rules ){
$GLOBALS['reigel_page_rewrite_rules'] = $page_rewrite_rules;
return array();
}

add_filter( 'rewrite_rules_array', 'reigel_prepend_page_rewrite_rules' );
function reigel_prepend_page_rewrite_rules( $rewrite_rules ) {
return $GLOBALS['reigel_page_rewrite_rules'] + $rewrite_rules;
}


you may need to visit dashboard, Settings > Permalinks too for this to work.


Reigel Gallarde comments:

@guys that says it can be done... try to reproduce and use my code...


Reigel Gallarde comments:

@guys that says it <strong>can't</strong> be done... try to reproduce and use my code...


Nick comments:

Nice Reigel, can you explain what the code does?


Reigel Gallarde comments:

I'm not really sure how to explain this... I was just in the same situation before and this code fixed my problem... if I can remember it correctly, this has something to do with rewrite rules priority...

2016-03-04

Darlene Grace Arcenal answers:

You can't have a page and a custom post type with the same url/slug this will cause 404 problems. Try renaming your landing page/custom post type to a different one. These two can't have the same slug.


Nick comments:

Ok, any tips on rewrite rules etc?

2016-03-04

Andrea P answers:

you can't have both with the base /people/
and there are no rewrite rules you could make for that, because they will apply to both, as both have the same slug.. that's exactly the problem you are facing now. wordpress have built some rewrite rules to display the pretty-permalinks of the post_type and of that page/sub-pages, and those rules are crashing one against the other, as they are trying to apply to both.

the only way you can sort this is changing the slug of one of them. so you can:

1) change the slug of the page to be, by instance, "people-home"
- to do this, you simply edit your page, and you'll see the url below the title. change the slug there and you've done (you might have to go to permalink settings and click save, to refresh the rewrite rules)


2) change the rewriting slug of the post type to be, by instance, "person"
- to do this, change this row in your code:
'rewrite' => array('slug' => 'people', 'with_front' => false),
and put this
'rewrite' => array('slug' => 'person', 'with_front' => false),


2016-03-04

Rempty answers:

what do you show in the page people?


Rempty comments:

The Reigel Gallarde's code:
First enable the verbose permalinks
Second Save all the permalinks used by pages
Third Add them to the rewrite rules.

2016-03-04

Luis Abarca answers:

You should call flush_rewrite_rules() when the plugin is activated.

Or go to to Settings > Prrmalinks to refresh the rules.

2016-03-04

Bob answers:

Darlene Grace Arcenal is right.

url with same slug will cause a trouble.