0

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.

5
  • Where is elegant_series_save_post_series_select_meta called? Commented Jul 25, 2023 at 11:36
  • I have an action add_action( 'save_post', 'elegant_series_save_post_series_select_meta', 10, 2 ); Commented Aug 16, 2023 at 6:11
  • Which is inside elegant_series_post_meta_boxes_setup function which is added through those two add_action( 'load-post.php', 'elegant_series_post_meta_boxes_setup' ); add_action( 'load-post-new.php', 'elegant_series_post_meta_boxes_setup' ); Commented Aug 16, 2023 at 6:13
  • elegant_series_save_post_series_select_meta is hooked to save_post, and calls wp_update_post, which triggers save_post, leading to an infinite loop. See: wordpress.stackexchange.com/questions/51363/… Commented Aug 16, 2023 at 8:01
  • 1
    Does this answer your question? How to avoid infinite loop in save_post callback Commented Aug 16, 2023 at 8:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.