Question

I have been using this site alot for solving my problems, but this time I did not succeed in finding and answer for my problem.

I get and infinite loop when using wp_update_post inside a function that is called on save_post. I know that this is a common issue, but I can't figure out how to avoid it.

I want to save the order of my posts (which is of the post-type 'section'). So I have made a custom meta box which contain some sortable html-elements. In each element there is an hidden input tag with name='sectionorder[]'. So when I click the standard WordPress 'Update' buttom, an array containing all the ID's of the posts (in order) are send via POST. So here is the code where I retrieve the array, and wants to save the order:

    // Update section sort order
$sectionorder = $_POST['sectionorder'];
if (isset($sectionorder)) { // Avoid error if there is no sections added yet
    foreach( $sectionorder as $no => $sectionID ) {
        $post_update = array();
        $post_update['ID'] = $sectionID;
        $post_update['menu_order'] = $no;
        wp_update_post( $post_update );
    }
}

But the problem is that it starts an infinite loop. How do I avoid that? Maybe I can do it in a completely different way?

Appriciate your help!

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top