Skip to content

Instantly share code, notes, and snippets.

@brandonkramer
Created October 24, 2020 00:16
Show Gist options
  • Save brandonkramer/9864944c7282bc740d8b6cae9e13677f to your computer and use it in GitHub Desktop.
Save brandonkramer/9864944c7282bc740d8b6cae9e13677f to your computer and use it in GitHub Desktop.
Change Yoast canonical URL in bulk for all posts for consolidation/migration by updating the meta field
add_action( 'wp_head', function () {
if ( isset( $_GET[ 'bulk-edit-canonical-url' ] ) ) {
$args = [
'post_type' => 'post',
'suppress_filters' => true,
'posts_per_page' => -1,
'post_status' => [ 'publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit' ],
];
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
update_post_meta( get_the_ID(), '_yoast_wpseo_canonical', str_replace( 'old-url.com', 'new-url.com', get_the_permalink() ) );
endwhile;
wp_reset_postdata();
}
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment