0

I'm having an issue with using wp_insert_post. I'm adding the ability for a post of one type to create a post of another type with the first post as the post parent. I was testing out a few things using the save_posts filter. I created a function that simply creates a post and then hooked that function to the save_posts filter. The problem I'm having is that it is adding posts exponentially to my mySQL table. The longer I let it run before I put a freeze on the server, the more posts get added. Is there a better way of doing this?

Example code:

public function save() {
    $my_child = array(
                'post_title' => $this->_child_type,
                'post_content' => "test content",
                'post_status' => 'publish',
                'post_type' => "video",
                'post_parent'=> 55
              );
    $nindex = wp_insert_post($my_child);
}
add_action('save_post', array(&$this, 'save'));

3 Answers 3

2

you can check what post type is calling the 'save_post' action try:

public function save() {
global $post;
  if (!$post->post_type = 'video'){
    $my_child = array(
                'post_title' => $this->_child_type,
                'post_content' => "test content",
                'post_status' => 'publish',
                'post_type' => "video",
                'post_parent'=> 55
              );
    $nindex = wp_insert_post($my_child);
  }
}
add_action('save_post', array(&$this, 'save'));
1
  • Thank you. Of course as things go, an hour after I posted this, I found my own solution., posted below Commented Feb 16, 2011 at 11:34
2

It might be because of revisions - if you've got revisions enabled, then this post suggests the save happens twice on purpose - once for the revision, and once for the actual post. Answer in that post (checking for type='revision') seems like the best solution.

2

Thanks to @BAInternet for his solution. The problem it seems is that because I'm creating a post, the save function gets called twice, so the number of posts increases exponentially. The solution that I came up with is:

public function save() {
    global $post;
    global $flag;
    //Following code makes sure it doesn't get executed twice
    if($flag ==0) $flag =1;  
    else return;
    //Next to temporarily disable this filter
    remove_action('save_post', array(&$this, __FUNCTION__));
    $my_child = array(
                'post_title' => $this->_child_type,
                'post_content' => "test content",
                'post_status' => 'publish',
                'post_type' => "video",
                'post_parent'=> 55
              );
    $nindex = wp_insert_post($my_child);
    }
    //restore save
    add_action('save_post', array(&$this, __FUNCTION__));
}
add_action('save_post', array(&$this, 'save'));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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