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

后端 未结 2 2116
我寻月下人不归
我寻月下人不归 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

    This shouldn't be happening in the first place, so you shouldn't try to fix it programmatically - instead you should fix it at source. Try to identify the cause and fix it. Otherwise you could introduce other issues down the line.

    Some possible solutions, depending on the cause:

    1. Flush your Rewrite Cache
      Wordpress doesn't write redirections into the .htaccess, it uses rewrite rules to parse the url and find a match for the redirection. It means that if you don't refresh your rewrite rules, the old links still work. Ref: SarahCoding's answer to 'Remove Old Permalinks?'

      How to do it:
      Re-saving your permalinks will flush the rewrite rules, but there if that doesn't work there are Three Ways to Flush the Rewrite Cache in WordPress
       

    2. Clear your Cache
      If you have an Caching plugins installed, they will need to be cleared. Some security plugins also use caching e.g. Securi. It could also just be cached in your browser.

      How to do it:
      See How to Clear Your Cache in WordPress
       

    3. Delete old WP permalinks
      When you update a slug, the old permalinks are still stored in the database. This could cause issues if you want to use a slug that you had previously used for example.

      How to do it:
      The old permalinks are stored in the table postmeta with the meta_key of _wp_old_slug. To clear all of the old slugs, run this query in your WP database:
      DELETE FROM wp_postmeta WHERE meta_key = '_wp_old_slug';

      Ref Mark Dave Tumanda's answer to 'Remove Old Permalinks?'
       

    4. Check Redirection Plugins
      If you are using any redirection plugins, check the redirection rules in case there is anything there that is clashing with your new urls.

    0 讨论(0)
  • 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()

    0 讨论(0)
提交回复
热议问题