0

Good evening!

I'm trying to adjust the post title + some ACF fields after a user have submitted a custom post type ('booking').

The function I've created almost works! The only problem is that wp_update_post() breaks it...

Adding wp_update_post() to the function will

  1. Reset the fields stored by ACF
  2. Remove the chosen category
  3. Obstruct the redirect to the new post, resulting in "Fatal error: Maximum execution time of 90 seconds exceeded"

What it does though is producing the correct post title! :)

I haven't really managed to understand the logic of action / filter / hooks in Wordpress yet, and therefore would appreciate some help.

Here's the code:

function booking_meta( $post_id, $post, $update ) {

if ( get_post_type( $post_id ) !== 'booking' ) { return; }

// Update ACF fields
$object_id = get_the_ID();
$owner_id  = get_the_author_meta( 'ID', $post->post_author );
update_field( 'object', $object_id, $post_id );
update_field( 'owner', $owner_id, $post_id );

// Create post title
$first_name = wp_get_current_user()->user_firstname;
$new_title = get_the_title() . ' to ' . $first_name;
$post_data = array(
        'ID'         => $post_id,
        'post_title' => $new_title,);

wp_update_post( $post_data );

}
add_action( 'wp_insert_post', 'booking_meta', 99, 3 );

I have already tried this without knowing why:

remove_action( 'wp_insert_post', 'booking_meta' );
wp_update_post( $post_data );
add_action( 'wp_insert_post', 'booking_meta' );

That way my first problem is solved but the others persists. It also makes some required datepicker fields in the form turn blank in the resulting post.

2
  • The wp_update_post calls the wp_insert_post action, which means your code gets stuck in an endless loop. You need to use remove_action() to remove booking_meta from the hook before you call wp_update_post() Commented Dec 5, 2023 at 22:14
  • Thank you @JacobPeattie! I had tried that already actually, without knowing why... Added that info + result in my question.
    – joxyzan
    Commented Dec 7, 2023 at 9:38

1 Answer 1

0

Found the answer thanks to Jacobs comments + this similar thread:

I had to use remove_action() WITH matching priority, like this:

function booking_meta( $post_id, $post, $update ) {

if ( get_post_type( $post_id ) !== 'booking' ) { return; }

// Update ACF fields
$object_id = get_the_ID();
$owner_id  = get_the_author_meta( 'ID', $post->post_author );
update_field( 'object', $object_id, $post_id );
update_field( 'owner', $owner_id, $post_id );

// Create post title
$first_name = wp_get_current_user()->user_firstname;
$new_title = get_the_title() . ' to ' . $first_name;
$post_data = array(
        'ID'         => $post_id,
        'post_title' => $new_title,);

remove_action( 'wp_insert_post', 'booking_meta', 99, 3 );
wp_update_post( $post_data );

}
add_action( 'wp_insert_post', 'booking_meta', 99, 3 );

Your Answer

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

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