Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have code which takes some $_POST data and loops through it. In the loop, I store the $_POST data in variables and create a new post using those variables. The loop ends when I run out of $_POST data.

All of this works. I even output the variables during the loop so I know the loop works and I see the created posts on my site.

The problem is, while the outputted variables only appear once (as the should), I'm seeing doubles of all the posts my code creates.

Here is the post data:

Array ( [scrape] => Array ( [0] => Array ( [title] => Sample 1   [url] => http://google.com [1] => Array ( [title] => Sample 2   [url] => http://ebay.com [2] => Array ( [title] => Sample 3   [url] => http://test.com [3] => Array ( [title] => Sample 4   [url] => http://codingrocks.com ) ) ) 

Here is my code:

$insDate = date('Y-m-d H:i:s');
$count = count($_POST['scrape']);
$i=0;
while($i<$count){
    $title = $_POST['scrape'][$i]['title'];
    $url = $_POST['scrape'][$i]['url'];
    echo "Title: ".$title."<br>";
    echo "URL: ".$url."<br>";
    $i++;
    echo "<br>";

// Above variables echoed to show what is being sent - they appear once (as the should) per post
// Below is the code to create the new posts. The posts are being created TWICE (should be once)

    $my_post = array(
        'post_title'    => $title,
        'post_date'     => $insDate,
        'post_content'  => '$url',
        'post_status'   => 'publish',
        'post_type'     => 'post',
    );

    $the_post_id = wp_insert_post( $my_post );

I did see this post: Plugin Development: Wordpress processes twice on post update. How to skip process on the first?

But I'm not using a custom function to post, I'm using wordpress' built-in function which means the solution offered there doesn't apply to my case... or does it?

The process:

I scrape a page and store the data into variables. These variables are then place into a form which the user can edit. The form consists of fields for as many posts as the scraper can find variables for. When the user submits the form, the above $_POST data is sent to parsed.php which contains the above code which creates the posts.

So my question is, why is it creating posts twice(when it shouldn't and how do I fix it?

share|improve this question
add comment

2 Answers

The code does not adds posts twice - checked in my local installation.

You have got errors in it:

$my_post = array(
    'post_title'    => $title,
    'post_date'     => $insDate,
    'post_content'  => '$url',
    'post_status'   => 'public',
    'post_type'     => 'post',
);

Should be

$my_post = array(
    'post_title'    => $title,
    'post_date'     => $insDate,
    'post_content'  => $url,
    'post_status'   => 'publish',
    'post_type'     => 'post',
);

Otherwise, the problem will be in the form posting the variables. Maybe it get's redirected and is submited twice, or something similar.

share|improve this answer
    
How could it possibly get redirected? There's nothing else in the code. –  Sweepster Jan 31 '13 at 22:11
    
I would be easier to answer your question being able to see or get idea of the whole proces desingn –  david.binda Jan 31 '13 at 22:27
    
See updated question. –  Sweepster Jan 31 '13 at 22:43
add comment

Found a solution. By wrapping my code inside

if ( true !== DOING_CRON && true !== DOING_AJAX ) {

}

The posts now only post once, as they should.

share|improve this answer
add comment

Your Answer

 
discard

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.