Title: Problem with wp_insert_post
Last modified: August 19, 2016

---

# Problem with wp_insert_post

 *  [elusuario](https://wordpress.org/support/users/elusuario/)
 * (@elusuario)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/problem-with-wp_insert_post/)
 * Hello, im very new in building WP Plugins.
 * I want to build a plugin that changes the title and the content before posting,
   but i have a problem with wp_insert_post. When i run this function, the script
   keeps running until Apache crashes (i think is an infinite loop).
 * This is the code:
 *     ```
       function one($post_id, $post) {
   
           $post->post_title = "new title";
           $post->post_content = "new comment";
           wp_insert_post($post);
   
       }
   
       add_action("publish_post", "one", 1, 2);
       ```
   
 * Any ideas?
 * Kind regards.

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Thread Starter [elusuario](https://wordpress.org/support/users/elusuario/)
 * (@elusuario)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/problem-with-wp_insert_post/#post-1212752)
 * The code is adding the post, but…
 * If i turn off the time limit (set_time_limit(0);), it keeps running infinitely;
   if not, it stops after 30 seconds and i get a fatal error.
 *  [E](https://wordpress.org/support/users/visudo/)
 * (@visudo)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/problem-with-wp_insert_post/#post-1213038)
 * the code gets stuck in an infinite loop. wp_insert_post calls publish_post which
   calls wp_insert_post so on and so on. I am looking for a clean way to resolve
   this issue as well. hopefully i won’t have to resort to wpdb calls.
 *  [wlk](https://wordpress.org/support/users/wlk/)
 * (@wlk)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/problem-with-wp_insert_post/#post-1213052)
 * Hi,
    I suggest you to do it other way. Because what you did does this:
 *     ```
       1. on publish_post do:
       2. add new post and call publish_post, which goes to step 1
       ```
   
 * Function one is ok, but the trigger that activates it is wrong (unless you want
   to have infinite loop of posts being created).
 * Find more appropriate hook for calling function that will create new post.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Problem with wp_insert_post’ is closed to new replies.

## Tags

 * [wp_insert_post](https://wordpress.org/support/topic-tag/wp_insert_post/)

 * 3 replies
 * 3 participants
 * Last reply from: [wlk](https://wordpress.org/support/users/wlk/)
 * Last activity: [16 years, 4 months ago](https://wordpress.org/support/topic/problem-with-wp_insert_post/#post-1213052)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
