2

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.

2
  • Did you try to update permalink setting after adding the code above?
    – Tung Du
    Sep 25, 2018 at 8:45
  • yes done. Only on removing the rewrite rule, the archive pages are working, else not.
    – Annapurna
    Sep 25, 2018 at 13:50

1 Answer 1

1

It 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':

  1. 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' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.