Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi @wppit,

    There is a question at WordPress StackExchange about this problem.

    This answer recommends using

    'rewrite' => array(
      'slug' => '/',
      'with_front' => false
    )

    but some users stated that this brings 404 errors on all other post types, even after saving permalinks.

    This other answer brings another solution (replace events with your post type slug:

    function na_remove_slug( $post_link, $post, $leavename ) {
        if ( 'events' != $post->post_type || 'publish' != $post->post_status ) {
            return $post_link;
        }
        $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
        return $post_link;
    }
    add_filter( 'post_type_link', 'na_remove_slug', 10, 3 );
    
    function na_parse_request( $query ) {
        if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
            return;
        }
        if ( ! empty( $query->query['name'] ) ) {
            $query->set( 'post_type', array( 'post', 'events', 'page' ) );
        }
    }
    add_action( 'pre_get_posts', 'na_parse_request' );

    Don’t forget to save your permalink configurations after that.

    I’d also recommend some approach to avoid pages and products with the same slug. I hope it helps!

    Login to your dashboard and go to Settings > Permalinks. From there you can remove the permalink base for posts so you don’t need to go into the code level. That’ll let you remove the post base for all post types too.

    Rewrite slug parameter defaults to $post_type key, @danieltj, as seen in register_post_type docs. Even without a permalink base for posts, the post type slug will be included in front of content slug. Right?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to create a custom post type without extra slug?’ is closed to new replies.