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' );