Why does wp_update_post causes white screen?

The question:

I made a custom post type, named “Foglalás”. Each post in the Foglalás post type has neither title nor permalink. All of them are kind of Auto Draft after saving. So, I would like to get the post ID and set it to be the title and permalink as well. I use the following code:

    function foglalasi_adatok( $post_id ) {
       $post = array(
        'ID'         => $post_id,
        'post_title' => $post_id,
        'post_name'  => $post_id,
       );
       
       wp_update_post( $post );
       update_post_meta($post_id, 'foglalas_szama', $post_id);
    }

add_action('save_post_foglalas', 'foglalasi_adatok');

When adding new post in this type, WordPress throw a blank page. When commenting out wp_update_post( $post ), the update_post_meta is working fine.

What do I do wrong?

EDIT: I checked and no plugin conflicts.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

When commenting out wp_update_post( $post ), the update_post_meta is
working fine.

Yes, the problem is not with update_post_meta(), but it is wp_update_post() because it uses wp_insert_post() which fires the save_post_foglalas hook (i.e. save_post_<post type>), and because your function calls wp_update_post(), then your function gets called infinitely and eventually caused the White Screen Of Death (WSOD). I.e. The hook calls your function, your function calls wp_update_post(), then the hook calls your function again, then the same things happen again and again and it never ends, or that it ended up with the WSOD..

Therefore if you hook a function onto that hook (or the save_post hook), and the function needs to call wp_update_post() or wp_insert_post(), then you need to unhook your function and re-hook it afterwards, e.g. after you’ve called wp_update_post():

// Unhook the function to avoid infinite loop.
remove_action( 'save_post_foglalas', 'foglalasi_adatok' );

// Then update the post, which calls save_post_foglalas again.
wp_update_post( $post );

// And re-hook the function.
add_action( 'save_post_foglalas', 'foglalasi_adatok' );

Additionally, as the WordPress Codex team stated in this comment, you should call wp_is_post_revision() to ensure that the post being updated is not a revision.

So here’s a full example based on your code:

function foglalasi_adatok( $post_id ) {
    // Do nothing, if the post is a revision.
    if ( wp_is_post_revision( $post_id ) ) {
        return;
    }

    // Unhook this function to avoid infinite loop.
    remove_action( 'save_post_foglalas', 'foglalasi_adatok' );

    $post = array(
        'ID'         => $post_id,
        'post_title' => $post_id,
        'post_name'  => $post_id,
    );

    wp_update_post( $post );

    update_post_meta( $post_id, 'foglalas_szama', $post_id );

    // Now re-hook this function.
    add_action( 'save_post_foglalas', 'foglalasi_adatok' );
}
add_action( 'save_post_foglalas', 'foglalasi_adatok' );


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment