1

I'm having some issues with some url structures on my current WP project.

I have a cpt and a page with the same name and URL structure. This obviously generates som issues. Since I have never really worked with the permalink structure of post vs pages I'm asking for some inputs on what choices I have.

I have a page called "ansatte" with a childpage called "bliv-frivillig".

Meanwhile I have a custom post type called ansatte with "ansatte" as its base url as well.

So when I try to access the "bliv-frivillig" childpage I'm greeted with a 404, since WP is looking for the post.

My dream scenario would be for both the pages and the cpt to share the base slug of "ansatte". Is this even possible in WP?

Here is my CPT args from functions:

$rewrite = array(
    'slug'                  => 'ansatte',
    'with_front'            => true,
    'pages'                 => false,
    'feeds'                 => false,
);
$args = array(
    'label'                 => __( 'Ansat', 'mydomain' ),
    'description'           => __( 'Ansatte og tilknyttede til Museer', 'mydomain' ),
    'labels'                => $labels,
    'supports'              => array( 'title', 'editor', 'thumbnail', ),
    'hierarchical'          => false,
    'public'                => true,
    'show_ui'               => true,
    'show_in_menu'          => true,
    'menu_position'         => 35,
    'menu_icon'             => 'dashicons-groups',
    'show_in_admin_bar'     => true,
    'show_in_nav_menus'     => true,
    'can_export'            => true,
    'has_archive'           => false,        
    'exclude_from_search'   => true,
    //'with_front'            => false,
    'publicly_queryable'    => true,
    'capability_type'       => 'page',
);

EDIT:

The custom post type "ansatte" doesn't actually need to have a single post page, since it will only be prevented in custom queries.

I realized i could set public => false, publicly_queryable => false and with_front => false, so now at least the posts are not displayed in the front end. But when i try to access the child page "bliv-frivillig" (http://domain.com/ansatte/bliv-frivillig) I'm redirected to the frontpage.

0

No,it is not possible. The reason behind this is that, the child page with the parent slug which is same as the cpt slug will be behaved as the cpt post and wp will search for that post rather than looking for the child page with same parent slug. And if you try the parent page, it will be replaced with the cpt archive page. This is based on the template hierarchy and how the WordPress handles the url for cpt, posts and pages.

  • Yeah, I was expecting this. Just hoping for some clever rewrite rule or something. I suppose since I'm not using any frontpage version of single post or archive for the cpt the URL for it doesnt matter. That's a solution for this exact dilemma, but not really the general issue with CPT and page urls – Emil Østervig May 1 '17 at 10:51
  • Sorry to say, there is no other way than to change either page or post-type slug. – Alok Shrestha May 2 '17 at 10:02

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.