Skip to content

Instantly share code, notes, and snippets.

@cameronbraid
cameronbraid / bulk-edit-yoast-canonical-url.php
Last active November 12, 2022 04:14 — forked from brandonkramer/bulk-edit-yoast-canonical-url.php
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() ) );