I have created a CPT with rewrite rules as follows:
"rewrite" => array( "slug" => "https://wordpress.stackexchange.com/", "with_front" => false ),
In the functions.php, I have added the following code:
function sh_parse_request_tricksy( $query ) {
// Only noop the main query
if ( ! $query->is_main_query() )
return;
// Only noop our very specific rewrite rule match
if ( 2 != count( $query->query )
|| ! isset( $query->query['page'] ) )
return;
// 'name' will be set if post permalinks are just post_name,
otherwise the page rule will match
if ( ! empty( $query->query['name'] ) ) {
$query->set( 'post_type', array( 'my_cpt1','my_cpt2' ) );
}
}
add_action( 'pre_get_posts', 'sh_parse_request_tricksy' )
I got this working to remove the CPT slug for my CPT URLs. But the issue here is, all my archive pages gives me 404 error. Even the author archive page is not working.
Can anyone help me resolve this?
Thanks in advance.
It took some time to figure out the issue, but I got it done.
Passing “https://wordpress.stackexchange.com/” for rewrite slug is not recommended, since it causes more problem than it solves, as in this case, causing 404 error in other pages.
So removing it was the first step.
Next, I had to write the following code in my ‘functions.php’:
-
To remove the slug from the published posts:
/**
* Remove the slug from published post permalinks. Only affect our CPT though.
*/
function sh_remove_cpt_slug( $post_link, $post, $leavename ) {
if ( in_array( $post->post_type, array( 'my_cpt1' ) )
|| in_array( $post->post_type, array( 'my_cpt2' )
|| 'publish' == $post->post_status )
$post_link = str_replace( "https://wordpress.stackexchange.com/" . $post->post_type . "https://wordpress.stackexchange.com/", "https://wordpress.stackexchange.com/", $post_link );
return $post_link;
}
add_filter( 'post_type_link', 'sh_remove_cpt_slug', 10, 3 );
This will still cause an error since it specifies that only ‘post’ and ‘page’ post types can have url without post-type slug.
Now to teach WP that out CPT will also have URL without slug, we need to get this in our functions.php:
function sh_parse_request( $query ) {
// Only loop the main query
if ( ! $query->is_main_query() ) {
return;
}
// Only loop our very specific rewrite rule match
if ( 2 != count( $query->query )
|| ! isset( $query->query['page'] ) )
return;
// 'name' will be set if post permalinks are just post_name, otherwise the page rule will match
if ( ! empty( $query->query['name'] ) ) {
$query->set( 'post_type', array( 'my_cpt1','my_cpt2' ) );
}
}
add_action( 'pre_get_posts', 'sh_parse_request' );