WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:

    //-- 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.

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

share|improve this question

try putting your script in a function and running it during an action hook. perhaps the wp_footer hook.

share|improve this answer

Specify a unique ID at the start of the theme. This will be an identification string in a custom field for the newly created post.

Before you add the post, search for posts with this unique ID in custom fields. If you don't find it, add the post AND the custom field with it as well.

Here you can find examples how to query posts by custom fields.

share|improve this answer

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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.