Created
January 10, 2024 22:18
-
-
Save topher1kenobe/e07b2705bf66d3d4eebcafeb7f22a282 to your computer and use it in GitHub Desktop.
Update slug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.