重写 URL 以排除 slug 后,如何删除旧的自定义帖子类型永久链接? [英] How do I delete the old Custom Post Type permalink after rewriting the URL to exclude the slug?

查看:22
本文介绍了重写 URL 以排除 slug 后,如何删除旧的自定义帖子类型永久链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 Book 的 Custom Post Type,链接是:mywebsite.com/book/mybookname

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

我想更改此链接,使链接为 mywebsite.com/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' );

问题是旧链接(mywebsite.com/book/mybookname) 仍然有效.我想让该链接转到 404 页面而不破坏当前链接.

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' );

如何删除旧网址?

推荐答案

这首先不应该发生,因此您不应该尝试以编程方式修复它 - 而是应该从源头修复它.尝试找出原因并修复它.否则,您可能会引入其他问题.

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. 刷新重写缓存
    Wordpress 不会将重定向写入 .htaccess,它使用 SarahCoding 对删除旧永久链接?"的回答

    怎么做:
    重新保存永久链接将刷新重写规则,但如果这不起作用,则有三种在 WordPress 中刷新重写缓存的方法
     

  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
     

清除缓存
如果您安装了缓存插件,则需要清除它们.一些安全插件也使用缓存,例如塞库里.它也可以只缓存在您的浏览器中.

如何操作:
请参阅如何清除 WordPress 中的缓存
 

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
 

删除旧的 WP 永久链接
更新 slug 时,旧的永久链接仍存储在数据库中.例如,如果您想使用以前使用过的 slug,这可能会导致问题.

怎么做:
旧的永久链接存储在postmeta表中,meta_key是<代码>_wp_old_slug.要清除所有旧的 slug,请在您的 WP 数据库中运行此查询:
DELETE FROM wp_postmeta WHERE meta_key = '_wp_old_slug';

参考 Mark Dave Tumanda 对删除旧永久链接?"的回答
 

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?'
 

检查重定向插件
如果您正在使用任何重定向插件,请检查重定向规则,以防有任何内容与您的新网址发生冲突.

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.

这篇关于重写 URL 以排除 slug 后,如何删除旧的自定义帖子类型永久链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆