Skip to content

Instantly share code, notes, and snippets.

@topher1kenobe
Created January 10, 2024 22:18
Show Gist options
  • Save topher1kenobe/e07b2705bf66d3d4eebcafeb7f22a282 to your computer and use it in GitHub Desktop.
Save topher1kenobe/e07b2705bf66d3d4eebcafeb7f22a282 to your computer and use it in GitHub Desktop.
Update slug
// change slug on post if title is changed.
add_action( 'save_post', 'icput_post_title_fix' );
function icput_post_title_fix( $post_id ) {
$url = rwmb_get_value( 'canonical_url', '', $post_id );
if ( empty( $url ) ) {
return;
}
if ( get_post_type( $post_id ) === 'post' ) {
$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 );
}
}
}
@topher1kenobe
Copy link
Author

rwmb_get_value is from MetaBox and it's a wrapper for get_post_meta. The host sent me some logs that seem to indicate that it's timing out on this file. There are 3400 posts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment