• I’m getting a really strange thing. 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?

Viewing 12 replies - 1 through 12 (of 12 total)
  • This must be firing twice somehow. Echo something right before the wp_insert_post() call and see if it doesn’t print twice.

    Thread Starter TheCosmonaut

    (@thecosmonaut)

    Thanks for the reply! Unfortunately it looks like whatever is causing it to fire twice is not in the single.php page. This is the entire code I’m using on my test page:

    <?php get_header(); ?>
    
    	<?php $postTest = 0;
    	echo '<p>LOAD: $postTest='.$postTest.'</p>'; ?>
    
    	<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    		<?php
    
    		//-- Set up post values
    		$myPost = array(
    			'post_status' => 'publish',
    			'post_type' => 'post',
    			'post_author' => 2, // should be this post's Author
    			'post_title' => 'e3i8ir',
    			'comment_status' => 'closed',
    			'ping_status' => 'closed',
    			'post_category' => array(24), // should be the NEXT category
    		);
    
    		echo '<p>BEFORE: $postTest='.$postTest.'</p>'; //test
    
    		//-- Create the new post
    		$newPostID = wp_insert_post($myPost);
    
    		$postTest++;
    		echo '<p>AFTER: $postTest='.$postTest.'</p>'; //test
    
    		?>
    	<?php endwhile; else: ?>
    	<?php endif; ?>
    <?php get_footer(); ?>

    Output is:

    <p>LOAD: $postTest=0</p>
    <p>BEFORE: $postTest=0</p><p>AFTER: $postTest=1</p>

    No duplication ??

    Any ideas what could cause this function to fire twice?

    Thanks,

    –eric

    Thread Starter TheCosmonaut

    (@thecosmonaut)

    I should mention also that I have no plugins activated…

    Thread Starter TheCosmonaut

    (@thecosmonaut)

    OK – now something REALLY weird:

    If I test the above code on Mac FF4.0, I get TWO posts.
    If I test the above code on Mac Safari 5.0 and Chrome 11.0, I get ONE post.

    WTF?!

    Thread Starter TheCosmonaut

    (@thecosmonaut)

    OK – confirmed. This “double-loading” behavior only happens on MY copy of Mac Firefox 4.0 — tested on FF4 on another computer, as well as Safari & Chrome and only one post happens. My only guess is that one of my extensions is somehow causing it to fire twice (don’t have any idea which one; has anybody experienced anything similar to this using Firefox?).

    What a relief — I thought I was going insane.

    No. I haven’t noticed that with FF4. Start disabling addons until the problem stops. I’d be interested in knowing which one is at fault.

    Thread Starter TheCosmonaut

    (@thecosmonaut)

    The really strange this is that I restarted my FF4 in Safe Mode (all add-ons off) and I’m still getting the duplication!

    I’m getting a similar problem creating posts from db entries.

    The following code is inside a foreach loop, within a function in functions.php:

    echo $post_content;
    $new_post = array(
    	'post_title' => $post_title,
    	'post_content' => $post_content,
    	'post_status' => 'publish',
    	'post_date' => date('Y-m-d H:i:s'),
    	'post_author' => $user_ID,
    	'post_type' => 'post',
    	'post_category' => array($prod_id)
    );
    $post_id = wp_insert_post($new_post);

    The echo displays on-page once per row from db, as it should. But the wp_insert_post() call is creating 3 posts, per row.

    This is in FF3.6 mac. After reading above posts, tried it in safari mac, but exactly the same thing happens (3 of each post).

    Just found a solution that appears to have worked for me, here.

    Basically it involves adding some unique meta-data to each post and checks this before calling wp_insert_post.

    As for the op’s browser issues… I have no idea!! ??

    See this ticket #11081 on trac; it was marked invalid, but there is an explanation and a solution there.

    In short, try wrapping your codde up like this to prevent your code firing during cron and ajax; wp_insert_post triggers cron, causing duplicate posts.

    if ( true !== DOING_CRON && true !== DOING_AJAX ) { // Do your stuff }

    Hi, I’m using wp_inset_post in AJAX

    So this code is not working .
    if ( true !== DOING_CRON && true !== DOING_AJAX ) { // Do your stuff }

    Any idea to stop my duplicate posts ?
    Thks a lot

    did anyone ever find an answer to this? I think I’m having the same problem. 2 posts are being cached by google for each of my posts, which have 2 different titles and are very slightly different (one has weird in-line css styles applied to some elements).

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘How to prevent duplicate posts with wp_insert_post on single.php’ is closed to new replies.