• I work at a radio station and have built a plugin that automatically builds a playlist on our site from what plays on the radio. It starts by querying an XML playlist file output by our radio streaming server. If the plugin has not seen a song played at a specific time before, it will create a new post (itself a custom post type called “Song”) for that XML song entry. Then I have a shortcode that just runs a WP_Query for posts of type “Song” and outputs the most recent.

    The problem is this: the XML is queried every time someone opens a page that calls the shortcode. When we get a lot of people visiting our site, we will end up with duplicate posts of the same song at the same time. I presume this is because two people happened to query the XML file and the WP_Query at the same time, which resulted in neither of them finding the song and so both of them adding the post. I’m trying to find a way to prevent this duplication from happening, but so far, I haven’t come up with much. Any ideas would be appreciated.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    If you re-query the data base for duplicate posts immediately before a new post, it would minimize duplicates. Or maybe you are doing this? I don’t fully understand what’s happening when in your scenario.

    It should be possible to invoke exclusive write access to the posts table while your plugin checks for duplicates and makes a new post. If WP doesn’t have this sort of privilege, your plugin could establish it’s own lock system by storing a lock value as an option to prevent other instances of itself from writing at the same time. You may need to do a cache flush to ensure the lock value is valid in real time and not a cached value.

    Thread Starter jklapel

    (@jklapel)

    bcworkz, you are correct, I am already doing this. I run a WP_Query and then the very next line is an if statement that leads into an insert_post statement so there is very little time between query and post insertion.

    I haven’t seen any sort of write locking in WordPress, though I have been looking. I was hoping to avoid writing my own database access methods, but it looks like I just might have to do that. I also tried to use PHP semaphores, but then I found out that the shared hosting I am on doesn’t have the PHP semaphores package installed.

    If anyone has any further ideas, I’ll gladly accept them. Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Duplicate posts from custom import’ is closed to new replies.