I am building a WordPress
plugin the sets the post_parent
of a post to the id of a custom post type called series. However, once I click update
on the post it successfully updates the post and the next request hangs for 40 seconds on my laptop and never loads on my PC even if I reload or open a new tab. On my PC I have to restart Apache
to fix it. Here is relevant the code:
/**
* Saves part of which series the post is.
*
* @param $post_id
* @param $post
* @return mixed
*/
function elegant_series_save_post_series_select_meta($post_id, $post) {
/* Verify the nonce before proceeding. */
if ( !isset( $_POST['elegant_series_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['elegant_series_meta_box_nonce'], 'save-elegant-series-meta-box' ) ) {
return $post_id;
}
/* Get the post type object. */
$post_type = get_post_type_object( $post->post_type );
/* Check if the current user has permission to edit the post. */
if ( !current_user_can( $post_type->cap->edit_post, $post_id ) )
return $post_id;
/* Get the posted data and sanitize it for use */
$series_id = ( isset( $_POST['elegant-series-select-series'] ) ? absint( $_POST['elegant-series-select-series'] ) : '' );
elegant_series_add_post_to_series($post_id, $series_id);
}
function elegant_series_add_post_to_series($post_id, $series_id)
{
wp_update_post(
[
'ID' => $post_id,
'post_parent' => $series_id
]
);
}
could this be due to having a lot of posts - I have about ~2500.
I tried googling various things but did not find anything.
elegant_series_save_post_series_select_meta
called?add_action( 'save_post', 'elegant_series_save_post_series_select_meta', 10, 2 );
elegant_series_post_meta_boxes_setup
function which is added through those twoadd_action( 'load-post.php', 'elegant_series_post_meta_boxes_setup' ); add_action( 'load-post-new.php', 'elegant_series_post_meta_boxes_setup' );
elegant_series_save_post_series_select_meta
is hooked tosave_post
, and callswp_update_post
, which triggerssave_post
, leading to an infinite loop. See: wordpress.stackexchange.com/questions/51363/…