I'm using an acf field to create a custom slug for a post type. Everything works and saves correct in the database. Only the shown value of the link field in the backend is not updated on publish. I need to reload the page to see the new custom slug. On save the link field value for a new slug is updated instantly.
Is there a way of updating the shown value in the backend even on first publish. Please note that everything works correctly it's just a cosmetic bug I can't fix without the end user having to reload the page.
This is my code so far:
add_action( 'save_post', 'wpse105926_save_post_callback'); function wpse105926_save_post_callback( $post_id) { $post = get_post($post_id); $target_post_types = ['song', 'album']; if (in_array($post->post_type, $target_post_types)) { // unhook this function to prevent infinite looping remove_action( 'save_post', 'wpse105926_save_post_callback' ); $artist_field = ($post->post_type === 'song') ? get_field('song_artist', $post->ID) : get_field('album_artist', $post->ID); if (is_array($artist_field) && !empty($artist_field)) $artist_slug = isset($artist_field[0]->post_name) ? $artist_field[0]->post_name : ''; elseif (is_object($artist_field)) $artist_slug = isset($artist_field->post_name) ? $artist_field->post_name : ''; else $artist_slug = ''; $post_title = sanitize_title_with_dashes($post->post_title); if ($artist_slug !== '') $slug = "{$post_title}-{$artist_slug}"; else $slug = "{$post_title}"; // update the post slug wp_update_post( array( 'ID' => $post_id, 'post_name' => $slug // do your thing here )); // re-hook this function add_action( 'save_post', 'wpse105926_save_post_callback' ); } } I found this post with an explanation but it seems strange because on follwing saves it works instantly: Post slug changed using code doesn't reflect on editor when post is published
wp_update_posta second time, could this be simplified to instead filter the parameters passed in before the post is created/inserted/updated viathewp_insert_post_data` hook? developer.wordpress.org/reference/hooks/wp_insert_post_data This would also avoid the problem of infinite looping that requires unhooking/rehooking. Also$slug = "{$post_title}"is just$slug = $post_title