Created
October 24, 2020 00:16
-
-
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
This file contains hidden or 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
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