MetaBox using wp_insert_post equals infinite loop
-
I have a metabox that’s displayed on the Add post and Add pages screens. When the user adds data to the box, after writing their article, and then clicks publish, two things should happen;
1. The post/page is saved as a new post/page. Standard processes and all.
2. The metabox save code attempts to also save a different post/page with a new title and in draft status. That code comes to this;if (!get_page_by_title($trimmedidea)) { $my_post = array( 'post_status' => 'draft', 'post_type' => 'post', 'post_author' => $user_ID, 'post_title' => $trimmedidea, 'post_content' => 'Add your wonderful content!'); wp_insert_post( $my_post );
The problem is that somehow this goes into an infinite loop and I get a few thousand new posts. From what I’ve found online is that wp_insert_post calls something that then calls back wp_insert_post and the loop occurs. I’m guessing this is happening because it’s within a the code for saving the full “Add Post” code.
Therefore, I figure I’ve got two options…
1. Some obvious easy workaround that someone here knows
2. Code it as a mySQL insert statement.I’d rather use the natural WP processes so the table(s) are populated correctly…but I’m open to anything.
- The topic ‘MetaBox using wp_insert_post equals infinite loop’ is closed to new replies.