WordPress.org

Make WordPress Core

Opened 4 weeks ago

Last modified 3 weeks ago

#48410 new defect (bug)

`wp_insert_post()` can result in duplicate post data

Reported by: henry.wright Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Posts, Post Types Keywords: reporter-feedback
Focuses: Cc:
PR Number:

Description

If 2 identical calls to wp_insert_post() are made close enough together, 2 posts are added to the database instead of 1. These posts differ by post ID only. The slug for example is the same.

This seems to happen because wp_insert_post() does a fair amount of processing before the database write takes place. The check to determine if a post already exists can be _stale_ at the point of insertion.

Change History (4)

#1 @davidbaumwald
4 weeks ago

  • Component changed from General to Posts, Post Types

#2 @davidbaumwald
4 weeks ago

@henrywright Thanks for the ticket! Can you provide a bit more background on the use-case in this instance where you're having some concurrency of post insertions? A code sample would be really helpful as well.

#3 @desrosj
4 weeks ago

  • Keywords reporter-feedback added

#4 @henry.wright
3 weeks ago

Sure @davidbaumwald

If a website user requests a post by visiting the post permalink and the post doesn't exist in our WordPress install, we send a request to an internal system to see if data is available to import. If data is available the internal system responds with the data and wp_insert_post() is called immediately after checking again if the post exists

The duplicate issue happens when we are under heavy load and wp_insert_post() is called with the same arguments in a short space of time

Note: See TracTickets for help on using tickets.