Nate F.
Nate F.

Reputation: 584

Wordpress wp_insert_post causing Infinte Loop

I'm developing a Wordpress Plugin, which takes a PitchEngine JSON feed, and adds each 'Pitch' as a Wordpress Post.

I am able to retrieve the 'Pitch' contents, however the bellow function which adds the 'Pitch' as a post, results in an infinite loop, which adds an infinite number of posts. I've tracked it back to the wp_insert_post function. Without it, there isn't an infinite loop, with it, there is...

function pitchengine_create_post($jsonvals, $post_type = 'post') {

//Create Post



    $post = array(

          'comment_status' =>  'closed',
          'ping_status'    => 'closed', 
                      'post_content'   => $jsonvals->Text, 
          'post_date'      => $jsonvals->PublishDate, 
        'post_excerpt'   => $jsonvals->Summary, 
          'post_status'    =>  'publish',
          'post_title'     => $jsonvals->Headline, 
          'post_type'      => $post_type,           

          );  


    $post_id = wp_insert_post( $post, $wp_error );  

//Add Post Meta

        //pitchengine ID [DisplayUrl]
        add_post_meta($post_id, 'pitch_ID', $jsonvals->DisplayUrl);

        //pitchengine URL (brand Url base) + [DisplayUrl]
        add_post_meta($post_id, 'pitch_URL', $jsonvals->Meta->shorturl);

        //source name (pitchengine) 
        add_post_meta($post_id, 'pitch_name', 'Pitch Engine');




 //If error, return error

 //If success set return post ID
 $response = $post_id;

 return $response;

 }

Any ideas where I might be going wrong?

Upvotes: 0

Views: 1329

Answers (2)

Yatendra
Yatendra

Reputation: 1316

save_post and publish_post action work at all the time where you publish or update posts. Therefor your function call every time. To solve this your problem you can use CURL function. when you fetch feeds, post this data on a php page(Using Curl) where you write code for insert post.

I Hope it will solve your problem.

Upvotes: 0

Nate F.
Nate F.

Reputation: 584

Figured this one out:

I was testing this function by adding it to the save_post action, which is called every time wp_insert_post() is run. This created my infinite loop. To resolve this I removed the function from the save_post hook, and hooked the function to a different action that is unrelated to the wp_insert_post function.

Upvotes: 1

Related Questions