posts – How to remove slug from CPT correctly?

Question

Im trying to remove slug from my Custom Post Types. I’ve added

'rewrite' => array(
    'slug' => "https://wordpress.stackexchange.com/"
)

and based on this post https://wordpress.stackexchange.com/a/144356/116847 I’ve added:

add_action( 'pre_get_posts', 'wpse_include_my_post_type_in_query' );
function wpse_include_my_post_type_in_query( $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;

      // Include my post type in the query
     if ( ! empty( $query->query['name'] ) )
          $query->set( 'post_type', array( 'post', 'page', 'physio_methods', 'treatment' ) );
 }

 add_action( 'parse_query', 'wpse_parse_query' );
function wpse_parse_query( $wp_query ) {

    if( get_page_by_path($wp_query->query_vars['name']) ) {
        $wp_query->is_single = false;
        $wp_query->is_page = true;
    }

}

otherwise, posts was mixed with pages and gets 404. But now, I have problem with posts categories – link to category display post, not the archive page.

I need to fully remove slug from my CPT. Is there any good (no plugin) solution for that?

Thanks!

0
D_P 1 month 2023-02-17T13:58:25-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse