remove custom post type slug from url

Solutions on MaxInterview for remove custom post type slug from url by the best coders in the world

showing results for - "remove custom post type slug from url"
Mario
15 Jul 2017
1function na_remove_slug( $post_link, $post, $leavename ) {
2
3    if ( 'events' != $post->post_type || 'publish' != $post->post_status ) {
4        return $post_link;
5    }
6
7    $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
8
9    return $post_link;
10}
11add_filter( 'post_type_link', 'na_remove_slug', 10, 3 );
12
13function na_parse_request( $query ) {
14
15    if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
16        return;
17    }
18
19    if ( ! empty( $query->query['name'] ) ) {
20        $query->set( 'post_type', array( 'post', 'events', 'page' ) );
21    }
22}
23add_action( 'pre_get_posts', 'na_parse_request' );