How do I delete the old Custom Post Type permalink after rewriting the URL to exclude the slug?

后端 未结 2 2117
我寻月下人不归
我寻月下人不归 2020-12-06 22:57

I have a Custom Post Type called Book, and the link is: mywebsite.com/book/mybookname

I want to change this so that the link is myweb

2条回答
  •  清歌不尽
    2020-12-06 23:14

    Based on your comment all you need then is to know if the url contains /book ... see below for the added snip-it:

    function books_theme_parse_request( $query ) {
        global $wp;
        $current_url =  home_url( $wp->request );
    
        if (strpos($current_url, "/book" == false)) { return; }
    
        if(isset($query->query['post_type']) && $query->query['post_type'] == 'book'){
            global $wp_query;
            $wp_query->set_404();
            status_header( 404 );
            get_template_part( 404 ); exit();
        }
    
        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', 'book', 'page' ) );
        }
    }
    add_action( 'pre_get_posts', 'books_theme_parse_request' );
    

    As people have mentioned reference

    A Warning About Admin Usage

    This filter can also be used to affect admin screen queries. Be sure to check if your modification is affecting your post edit screens. For example, just checking is_main_query() and is_post_type_archive('movie') will also change the query for the edit.php?post_type=movie admin screen, unless you also check for !is_admin()

提交回复
热议问题