0

I have custom post types which URLs are appearing like that and working just fine:

http://www.mywebsite.com/my-cpt-name


The registration code for the custom post type is:

$rewrite = array('slug' => 'my-cpt-type', 'with_front' => false);
$download_args = array(
    'labels'            => $labels,
    'public'            => true,
    'publicly_queryable'=> true,
    'show_ui'           => true,
    'show_in_menu'      => true,
    'query_var'         => true,
    'rewrite'           => $rewrite,
    'capability_type'   => 'product',
    'map_meta_cap'      => true,
    'has_archive'       => $archives,
    'hierarchical'      => false,
    'supports'          => array( 'title', 'editor', 'thumbnail', 'excerpt', 'revisions', 'author' ),
);
register_post_type( 'my-cpt-type', $download_args );

I filter with post_type_link to make them appear as the link written before.

function custom_type_links( $post_link, $post = 0 ) {
    if ( $post->post_type === 'my-cpt-type' ) {
        return home_url( $post->post_name . '/' );
    }
    return $post_link;
}

For each CPT, I have news that are currently accessed with this kind of URL:


This is pretty bad for seo. I would like to change them so they would be one these two:


I already used the pre_post_link filter to change the permalink structure and it's working just fine. But when I go to the URL, I get a 404 error.

I'm trying to work with add_rewrite_rule but I can't get it to work. This is what I tried :

add_rewrite_rule( '(.+?)/news/(.+?)', 'index.php?pagename=$matches[2]', 'top' );

It's probably the wrong way to do so. Is there something else that I'm missing?

  • Where did you hear that this is bad for SEO? I'd call it a myth, all the evidence suggests shorter URLs are what you want. Eitherway please update your question with your cpt registration code – Tom J Nowell Feb 23 '18 at 12:05
  • Sorry for the misunderstanding. We don't want these posts to be high in seo. We want to add a route, so that there is a hierarchy with what's really important to us. I edited the code :) Thanks for the help! – Emilien Schneider Feb 23 '18 at 13:45
0

In addition to setting has_archive you need to set rewrite when you define the CPT. I replaced $archives because your code snippet does not have it defined.

$rewrite = array('slug' => 'my-cpt-type', 'with_front' => false);
$download_args = array(
    'labels'            => $labels,
    'public'            => true,
    'publicly_queryable'=> true,
    'show_ui'           => true,
    'show_in_menu'      => true,
    'query_var'         => true,
    'rewrite'           => $rewrite,
    'capability_type'   => 'product',
    'map_meta_cap'      => true,
    // Set has_archive to the prefix you want, so the archive is in the right place
    'has_archive'       => 'my-cpt-name/news',
    // Also set rewrite to the prefix you want, which will affect individual posts.
    'rewrite'      => 'my-cpt-name/news',
    'hierarchical'      => false,
    'supports'          => array( 'title', 'editor', 'thumbnail', 'excerpt', 'revisions', 'author' ),
);
register_post_type( 'my-cpt-type', $download_args );

You will want to:

  1. unregister_post_type('my-cpt-type'); to unregister the CPT, otherwise WP will not honor your changes. You will not lose any content when un-registering, it just clears the permalinks.

  2. Re-register using the settings above, then visit your Settings > Permalinks page to flush rewrite rules. You should then have the structure you want - just be sure to clear your browser cache before you try hitting the new URLs.

  • Thanks for your answer! Are you sure about this? I want to keep my-cpt-type just the way it is, and just set its news posts to a sub-route /news/. Your answer seems to suggest to unregister my current post type. – Emilien Schneider Feb 26 '18 at 9:33

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.