I have created a CPT with rewrite rules as follows:
"rewrite" => array( "slug" => "/", "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.
I have created a CPT with rewrite rules as follows:
"rewrite" => array( "slug" => "/", "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.
Share Improve this question edited Sep 25, 2018 at 8:17 Varsha Dhadge 3411 gold badge2 silver badges14 bronze badges asked Sep 25, 2018 at 5:16 AnnapurnaAnnapurna 3342 silver badges10 bronze badges 2- Did you try to update permalink setting after adding the code above? – Tung Du Commented Sep 25, 2018 at 8:45
- yes done. Only on removing the rewrite rule, the archive pages are working, else not. – Annapurna Commented Sep 25, 2018 at 13:50
1 Answer
Reset to default 1It took some time to figure out the issue, but I got it done.
Passing "/" 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( '/' . $post->post_type . '/', '/', $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' );