0

I've checked multiple questions and can't seem to find the answer.

How can I update a list of URLs to either include "-google" at the end of the permalink, or set the URL to a custom url. My list of URLs is 100+ and I have what the URL is and what the URL should be.

For example:

https://examplesite.com/customer/customer-name

change to:

https://examplesite.com/customer/google-customer-name

I have already updated the URLs in the post_content using this below:

UPDATE wp_posts SET post_content = REPLACE (post_content,'https://examplesite.com/customer/customer-name','https://examplesite.com/customer/google-customer-name');

That worked for the post content, but it's not a solution to update the permalink of those pages, which is what I need.

  • OK, so what exactly are you trying to do? I understand that you want to change URLs of posts, but then you're saying, that you tried to replace them in post_content... I'm a little bit confused :( – Krzysiek Dróżdż May 30 '18 at 16:40
  • 1
    I know someone would suggest the SQL I already used to update the URLs in the content. What I need to do now is update my list of URLs with new URLs that I already have setup. I'm trying to do this without having to manually go to 100+ pages/posts to update their permalink. – radethdart May 30 '18 at 17:31
0

OK, so let's assume you have these URLs in some array (if you don't, you can easily prepare it using any developer text editor, I guess). Let's call that array $slugs and let's say it's defined like so:

$slugs = array(
    'customer-name' => 'google-customer-name',
    ...
);

So there are only slugs in there.

So now you'll have to modify the posts:

global $wpdb;
foreach ( $slugs as $old => $new ) {
    $wpdb->update(
        $wpdb->posts,
        array( 'post_name' => $new ),
        array( 'post_name' => $old )
    );
}

This will change the slugs, hence the permalinks, for these posts.

  • This looks good, but before I run this code, I only need to run it once and then I can delete it after. How should I run this PHP once and have the changes be permanent? – radethdart May 30 '18 at 17:43
  • Yes, you have to run it only once. There are many ways to run it, I guess... I add it in themes functions.php and wrap it with if ( isset($_GET['domystuff']) ) { ... here comes the code } then I just go to the page and add ?domystuff=1 to the URL. It's not pretty, but it's pretty quick way to run some code manually... – Krzysiek Dróżdż May 30 '18 at 17:46
  • Alright, I was able to test this out and it doesn't seem to be working. Here is a small sample of the code: <?php if ( isset($_GET['googlify']) ) { $slugs = array( '/customer-stories/madhive' => '/customer-stories/google-madhive', '/customer-stories/digital-first-media' => '/customer-stories/google-digital-first-media' ); global $wpdb; foreach ( $slugs as $old => $new ) { $wpdb->update( $wpdb->posts, array( 'post_name' => $new ), array( 'post_name' => $old ) ); } } ?> – radethdart May 30 '18 at 19:38
  • Of course your code won't work. But the problem is not with the code, but with your slugs array. Take a look at my code and on your code. What do slugs look like in my code and what do they look in your code? See any difference? ;) There should be no slashes in slugs. Also slug is only the last part of URL, not all of the URL. – Krzysiek Dróżdż May 30 '18 at 20:09
  • Well how can I keep that same structure. It's using a custom slugs plugin and the slug for instance, for the first one, is /customer-stories/madhive. The /customer-stories/ is part of it..it's even stored like that in the database. – radethdart May 30 '18 at 20:16
0

For permalinks have you tried to reset your permalink settings through admin panel? e.g. in Settings > Permalinks switch from Post name to Numeric and then back to Post name.

  • I'm not wanting to change ALL the urls, just a list of URLs that I have in a spreadsheet. – radethdart May 30 '18 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.