0

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

4
  • Gutenberg has an internal data store so that it knows what you're typing, and if the post needs saving, so when it goes to the REST API and sends the updated version, how would it know you've stepped in behind the scenes with a PHP filter and tweaked the slug to something else? Then when you refresh the page and the block editor loads that data from the database you see your modified slug Commented Oct 28, 2024 at 18:12
  • Given it also doesn't know you're calling wp_update_post a second time, could this be simplified to instead filter the parameters passed in before the post is created/inserted/updated via the wp_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 Commented Oct 28, 2024 at 18:18
  • I tried your second method and then the correct title only appears after saving a second time. How would I let the REST API know to update the display? I'm a bit at a loss right now Commented Oct 29, 2024 at 8:29
  • the REST API is fine, it's the block editors data store, it's unaware that what it sent is no longer ground truth, it'd require javascript changes. A JS based solution would be more robust here. Note though my comment would optimise what you have by avoiding another databse write Commented Oct 29, 2024 at 13:41

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.