Support » Developing with WordPress » Remove Slug From Custom Post Use Parent Post

  • scubageorge

    (@scubageorge)


    I am using this code to remove the slug from a custom post type. I cannot create a parent for that custom post type as it returns 404 error. Any assistance would be much appreciated!

    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' );
    • This topic was modified 2 months ago by  scubageorge.
    • This topic was modified 2 months ago by  scubageorge.
    • This topic was modified 2 months ago by  scubageorge.
Viewing 1 replies (of 1 total)
  • Howdy_McGee

    (@howdy_mcgee)

    Why are you removing the slugs from custom post types? If you want to manage the archive and single post slug rules you can do that on registration using the archive and rewrite parameters respectively when registering the post type.

    https://codex.wordpress.org/Function_Reference/register_post_type

    I think the problem you’re going to run into is that WordPress expects certain keywords in the URL rewrite scheme and when it can’t find those keywords it can’t tell what post type, page, or post you’re viewing which is why it’s running into 404 issues.

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.