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”.
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. 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.
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.
For instance, an object which I insert using wp_insert_post with the name 152912 gets the post_name of 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:
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.
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. This is intentionally, because it assumes that a numerical ID in the URL is the actual ID of the post. Adding the
-2makes sure that it is interpreted as a string and as such as the name of the post. Otherwise it would try to showhttp://yoursite/?p=152912and result into a 404 if this post id doesn’t exist.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.