Forked from brandonkramer/bulk-edit-yoast-canonical-url.php
Last active
November 12, 2022 04:14
-
-
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
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
| $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