Removing CPT slug from URL results in 404 error for archive page

Question

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.

0
, , , , Annapurna 3 years 2020-03-30T08:50:52-05:00 0 Answers 107 views 0

Leave an answer

Browse
Browse