Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
327 views
in Technique[技术] by (71.8m points)

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

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 mywebsite.com/mybookname.

I have added the following code to change the link and it works as expected:

function books_theme_remove_slug( $post_link, $post, $leavename ) {

    if ( 'book' != $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', 'books_theme_remove_slug', 10, 3 );

function books_theme_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', 'book', 'page' ) );
    }
}
add_action( 'pre_get_posts', 'books_theme_parse_request' );

The problem is that the old link(mywebsite.com/book/mybookname) is still working. I'd like to make that link go to a 404 page without breaking the current links.

I tried the following but it breaks everything:

function books_theme_parse_request( $query ) {
    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' );

How do I delete old url?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...