0

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.

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' );
3
  • 1
    Thank you for the explanation too!
    – Jani
    May 19 at 9:51
  • 1
    You're very welcome! PS: I've revised my answer with additional notes and code. 🙂
    – Sally CJ
    May 19 at 10:59
  • 1
    Thank you very much!!!
    – Jani
    May 20 at 8:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.