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:
- http://www.mywebsite.com/my-cpt-name/news/my-cpt-news-1
- http://www.mywebsite.com/my-cpt-name/my-cpt-news-1
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?