Why is WordPress creating permalinks with -2 at the end when no duplicate exists?

ChrisGreenhough used Ask the Experts™
Working on a WordPress site that is pulling post data from a third party system via an externally triggered Cron. Everything seems to be working absolutely fine, except that as posts are being created each post slug has -2 appended as if there were already a post with the same name - except there isn't. This doesn't always happen, however. If I zero the post tables in the db and run the custom post import plugin, everything is fine to begin with.

I had a feeling it might be something to do with being logged into the back end when the Cron task runs, but I can't prove this and am not sure where else to look for the issue or the solution.

Tried adding "if ( ! current_user_can( 'edit_posts' ) )" before the import function runs, but this didn't seem to cure it. Not sure how to proceed so any help appreciated.

Many thanks.

P.S. I'm not really a WordPress developer, more a designer who dabbles :)
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Jason C. LevineDon't talk to me.
Is there any pattern to it at all? Every X post gets the -2? Are any posts going to trash? Are you sure the source doesn't have duplicates?

If there is no pattern or other indicator, my best guess would be the script is firing more than once (i.e. restarting off a timeout or similar) and that's when and how the dupes appear.  

Check your error logs around the time the script runs to see if there is any weirdness.
PHP Web Developer
I have come across this, when it seemed there was no pre-existing post, but there was a post in "Trash" with the same name.


Thanks Jason and William for the valuable advice. I've spent endless hours coaxing this custom plugin to do what is required, even though I'm not actually a programmer and the third party that supplied it is a full time developer but gave us a crippled piece of software... Anyway, I won't get into that :) I appreciate the feedback. Rather than spend more time in the code, I'm going to try this plugin (https://wordpress.org/plugins/no-slug-conflicts-with-trash/) and hope that resolves it. I would never have thought to look for that plugin without the insights you offered, however, so my sincere thanks to you both.

Chris G.


Very helpful and a quick response to my need for assistance :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial