Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save cameronbraid/e91748f85d35625b5881f5825def45a6 to your computer and use it in GitHub Desktop.

Select an option

Save cameronbraid/e91748f85d35625b5881f5825def45a6 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
$oldPart = "https://old-url.com";
$newPart = "https://new-url.com";
$args = [
'post_type' => 'post',
'suppress_filters' => true,
'posts_per_page' => -1,
];
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
update_post_meta( get_the_ID(), '_yoast_wpseo_canonical', str_replace( oldPart, newPart, get_the_permalink() ) );
endwhile;
wp_reset_postdata();
$args = [
'post_type' => 'page',
'suppress_filters' => true,
'posts_per_page' => -1,
];
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
update_post_meta( get_the_ID(), '_yoast_wpseo_canonical', str_replace( oldPart, newPart, 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