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

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 :)
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jason C. LevineDon't talk to me.Commented:
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.
William NettmannPHP Web DeveloperCommented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ChrisGreenhoughAuthor Commented:
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.
ChrisGreenhoughAuthor Commented:
Very helpful and a quick response to my need for assistance :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.