Change WordPress slug on title change

I’m trying to post smaller pieces of code as they come up to better document things for myself and maybe it’ll end up helpful for others. So these things aren’t terribly exciting and may, in fact, be boring.

The Context

I set up a custom post type named “Cards” and another custom post type named “Updates” shows up under the cards. There’s a front-end Advanced Custom Fields form that automatically tags the update with the title of the card.

We had a problem where some of the updates were getting tagged but were not showing up where they should.

Turns out I had originally set the code to look for the slug (post_name) so I could use that in the show updates query more easily.1 The problem was that when a post is first saved the slug is generated at that point. If the title gets changed down the road, the slug doesn’t automatically update.

 updates_query_loop(get_post_field( 'post_name' ));

That was our problem. A post might initially be titled TRM Something and then be re-titled TRLM Something. It’s a hard thing to spot even if you knew to look at the slug portion of the permalink.

All the following code does is sync up the slug with the title. It’s hooked to the acf/safe_post action but could be tied to more any of the native WordPress functions.

//change slug on cards if title is changed
add_action('acf/save_post', 'cell_card_title_fix');
function cell_card_title_fix( $post_id ) {
	if(get_post_type($post_id)== "card"){ //is this a card? yes? . . . proceed
		$post = get_post($post_id);
		$title = $post->post_title;
		$clean_title = sanitize_title($title);
		$slug = $post->post_name;
		if($slug != $clean_title){
			 $clean_post = array(
		      'ID'           => $post_id,
		      'post_name'   => $clean_title,
		  );
		 
		// Update the post
		  wp_update_post( $clean_post );
		}

	}
}

1 Another option would have been to sanitize the title . . . but I liked the neatness of keeping the slug and title matching.

Comments on this post

  1. Taylor Jadin said on June 7, 2022 at 8:13 am

    Hello, yes. I’m hear for more code snippets.

  2. Alan Levine said on June 7, 2022 at 9:13 pm

    I am likely missing a key step but it seems fragile to reference by slug, why not use the post_id where you can then get the slug, title, etc as needed to do whatever it is the updates/cards are doing?

    • Tom Woodward said on June 15, 2022 at 7:16 am

      It definitely is fickle. What I should have done is create the term slug based on the post ID and then mapped the display name to the title. That would have been smarter. I wanted the tags to be legible to people and I didn’t think it through enough. It’s a pretty small group and I’ve already got a bunch of updates so I’ll probably let it ride. Next time I’ll be wiser . . . maybe.

Leave a Reply

Trackbacks and Pingbacks on this post

No trackbacks.

TrackBack URL