Support

Account

Home Forums Front-end Issues Updating the post_status causing problem in the backend

Solved

Updating the post_status causing problem in the backend

  • Hi,

    I have noticed when I try to update/create a new post from the back-end I get

    Fatal error: Maximum function nesting level of '100' reached, aborting! in C:\EasyPHP-12.1\www\oov3r\wp-includes\cache.php on line 113

    where the front end works just fine, after hours of searching for that bug it appears to be this line in function.php

    $post_id = wp_update_post( $post );

    which i use to update the states of the post, this is my updating code

    add_action( 'acf/save_post', 'update_existing_post_data', 10 );
    function update_existing_post_data( $post_id ) {
    
    	$post = array(
    		'ID'           	=> $post_id,
    		'post_status'  => $_POST['acf']['field_55dd90c79e8a6'],
    	);
    
    	$post_id = wp_update_post( $post );
    	
    	return $post_id;
    }

    and this is for adding new post:

    add_action('acf/save_post', 'my_save_post');
    function my_save_post( $post_id ) {
    
    	if ( ! ( is_user_logged_in() || current_user_can('publish_posts') ) ) {
    		return;
    	}
    
    	// check if this is to be a new post
    	if( $post_id != 'new_post' ) {
    		return $post_id;
    	}
    
    	// Create a new post
    	$post = array(
    		'post_status'		=> $_POST['acf']['field_55dd90c79e8a6'], // (publish, draft, private, etc.)
    	);
    
    	// insert the post
    	$post_id = wp_insert_post( $post );
    
    	//Save the fields to the post
    	do_action( 'acf/save_post' , $post_id );
    
    	return $post_id;
    }

    What Iam doing wrong? is there a better way to handle front end posting/editing ?

  • Hi @et3rnal

    I think that what you’re experiencing is the classic infinite loop problem.
    You hook into the saving of a post and in that function you save the post again thus calling the function over and over (and over and over).

    There’s information here about the issue and how to solve it:
    https://codex.wordpress.org/Function_Reference/wp_update_post

    Also, in your second function why are you calling

    
    //Save the fields to the post
    do_action( 'acf/save_post' , $post_id );
    

    ?

    It seems like you’re using the old code snippet for creating new posts (I assume you’re doing this with acf_form()). Make sure you read everything here:
    http://www.advancedcustomfields.com/resources/acf_form/

  • Hi Jonathan,

    After some testing I found that the second function is doing nothing!, everything going through the broken function (inc the status of a new post!), so i removed it.

    now this is what I have in function.php using the codex link u posted

    works perfectly fine

    //for new and updated post status 
    add_action( 'acf/save_post', 'update_existing_post_data', 10 );
    function update_existing_post_data( $post_id ) {
    
    	if ( ! wp_is_post_revision( $post_id ) ){
    	
    		// unhook this function so it doesn't loop infinitely
    		remove_action('save_post', 'update_existing_post_data');
    	
    		// Update existing post
    		$post = array(
    			'ID'           	=> $post_id,
    			'post_status'  => $_POST['acf']['field_55dd90c79e8a6'],
    		);
    		// update the post, which calls save_post again
    		wp_update_post( $post );
    
    		// re-hook this function
    		add_action('save_post', 'update_existing_post_data');
    	}
    }
    

    Please if you can see anything wrong with this point it out as this is not the first time I need to change this part again!.

  • Hi @et3rnal

    Nope that looks about right to me 🙂

  • No Problem!

    Best of luck in your project!

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Updating the post_status causing problem in the backend’ is closed to new replies.