简体   繁体   English

WordPress不断向永久链接添加-2

[英]Wordpress keeps adding -2 to permalinks

I'm currently building an object listing for a realtor and we are caching all the objects locally in a custom post type called "objekt", and it has the permalink rewrite "objekt". 我当前正在为房地产经纪人构建对象列表,并且我们将所有对象本地缓存在名为“ objekt”的自定义帖子类型中,并且其永久链接重写为“ objekt”。

We have a file in the root of the server called wp-fetch-items.php which the realtors application pings at the time of a new object in the system. 我们在服务器的根目录中有一个名为wp-fetch-items.php文件,房地产经纪人应用程序在系统中出现新对象时会对其执行ping操作。 This causes all posts of the type "objekt" to be deleted, and then it goes through an XML-file and adds all the objects to the system again. 这将导致删除“ objekt”类型的所有帖子,然后遍历XML文件并将所有对象再次添加到系统中。

Everything works perfectly fine, but the posts keep getting -2 in the URL, even if it's the first time the post is created, and there is nothing with a name even remotely similar. 一切工作都很好,但是即使是第一次创建帖子,帖子仍会在URL中保持-2的位置,并且名称之间几乎没有相似之处。

For instance, an object which I insert using wp_insert_post with the name 152912 gets the post_name of 152912-2 . 例如,我使用wp_insert_post插入的对象名称为152912的对象将获得post_name 152912-2

I create the objects with the following code: 我使用以下代码创建对象:

$insertedPost = wp_insert_post(array(
    'post_author' => 1,
    'post_type' => 'objekt',
    'ping_status' => 'closed',
    'post_status' => 'publish',
    'post_name' => $objectData['title'],
    'post_title' => $objectData['title']
), false);

And before they are created, I loop through every post with the type "objekt" and remove them using this code: 在创建它们之前,我遍历每条类型为“ objekt”的帖子,并使用以下代码将其删除:

wp_delete_post(get_the_ID(), true);

I specifically added true as a second parameter to permanently remove the object from the database and all searches I do prove that there is nothing left of it. 我专门添加了true作为第二个参数,以从数据库中永久删除该对象,并且我所做的所有搜索都证明没有任何剩余。

I also tried flushing the permalinks after deletion using this code, but it does nothing to help the situation: 我还尝试使用此代码在删除后刷新永久链接,但它无济于事:

global $wp_rewrite;
$wp_rewrite->flush_rules();

Have you experienced this issue, and do you know how to solve it? 您是否遇到过此问题,并且知道如何解决吗? Please reply if you know what could be causing this! 如果您知道是什么原因引起的,请回复!

Thanks in advance, Jonathan 预先感谢,乔纳森

Wordpress has a special handling of numerical post names. WordPress对数字帖子名称有特殊处理。 This is intentionally, because it assumes that a numerical ID in the URL is the actual ID of the post. 这是有意的,因为它假定URL中的数字ID是帖子的实际ID。 Adding the -2 makes sure that it is interpreted as a string and as such as the name of the post. 添加-2可以确保将其解释为字符串以及帖子的名称。 Otherwise it would try to show http://yoursite/?p=152912 and result into a 404 if this post id doesn't exist. 否则,如果该帖子ID不存在,它将尝试显示http://yoursite/?p=152912并导致输入404。

The only solution I found so far: avoid numerical names. 到目前为止,我发现的唯一解决方案是:避免使用数字名称。

(I wrote post because internally everything is a post, even pages and images) (我写帖子是因为内部所有内容都是帖子,甚至是页面和图像)

edit: 编辑:

I found the ticket that introduced this behaviour . 我找到了介绍这种行为票证

Check that you don't already have a post with that permalink. 检查您是否还没有该固定链接的帖子。 If the permalink already exists on that post, WordPress will append -2 to the permalink. 如果该帖子中已经存在永久链接,则WordPress会将-2附加到永久链接中。 Check your trash too. 也检查您的垃圾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM