[wp-hackers] Stopping recursion with save_post/wp_insert_post action

Mark E mark at simplercomputing.net
Sat Aug 21 04:06:22 UTC 2010


It's happening because you have a hook on wp_insert_post and in your 
hook function you call wp_insert_post. So it's going to keep going and 
going and going and going.

Looks to me like you don't really need to call wp_insert_post inside 
your hook function. Just add the post meta and be done with it - your 
hook function should be passed the post ID, so that's about all you need 
to add post meta. Or least that's how it looks to me based on the pseudo 
code you wrote.

Mark



On 08/20/2010 09:27 PM, Azizur Rahman wrote:
> Hi All,
>
> I am writing a plugin that hooks into wp_insert_post action.
>
> The problem is that I am going into recursion at the moment and not sure how
> to stop it.
>
> Here is the pseudo code logic
> *********************************************************************
> on wp_insert_post or save_post
>
> check the $post->content for x.
> return if  x is NOT found
>
> if x is found do
>     if x does NOT have meta key/value _x_details_ set
>         $x_details_id = wp_insert_post( $x_details);
>         add_post_meta($post_ID, '_x_details_', $x_details_id, true);
> end
> *********************************************************************
> I am sure you can see the recursion I am getting myself into when calling
> wp_insert_post( $x_details);
>
> what is the best way to stop this recursion?


More information about the wp-hackers mailing list