1

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.

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

1

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

0

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.

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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