How to prevent duplicate posts with wp_insert_post on single.php?

I’m getting a really strange bug (WP 3.1.3). My goal is to have users create a new post simply by landing on a single post. In order to make that happen, I have the following code in my single.php:

    <?php
    //-- Set up post values
    $myPost = array(
        'post_status' => 'publish',
        'post_type' => 'post',
        'post_author' => 2,
        'post_title' => 'e3i8ir',
        'comment_status' => 'closed',
        'ping_status' => 'closed',
        'post_category' => array(24),
    );

    //-- Create the new post
    $newPostID = wp_insert_post($myPost);

    ?>

By all rights, this should just insert one record, right? When I use this function, however, it creates TWO posts, exactly alike.

Read More

Looking at the page, the_content only displays once, so it’s not like the page is displaying multiple times (and therefore causing two wp_insert_post calls).

Does anyone have any ideas what would cause this?

Thanks in advance,
–eric

Related posts

Leave a Reply

3 comments

  1. a dirty fix maybe is having the post field marked as unique in the database – if first insert succeed, the second would fail

    this is a dirty fix till you find a more suitable solution