4

Trying to update GUID on a attachment using the wp_update_post() function but its not updating GUID nor its throwing any error.

Example code:

$post = [
    'ID'   => 45,
    'guid' => 'https://example.com/foo.png'
];
    
wp_update_post($post);
4
  • Why need to update guid?
    – birgire
    Mar 29, 2019 at 7:20
  • I'm not updating GUID for actual post but for attachment. I am planning to serve images from a image hosting site rather than serving from me. It works fine but I need to update GUID for that.
    – Harry
    Mar 29, 2019 at 7:25
  • Why? You are aware that the GUID has nothing to do with the post's permalink, right? It's just an identifier. Changing the GUID won't change a post's URL. Mar 29, 2019 at 11:24
  • I am not changing post permalink. It's for different thing. Anyway I figured it out.
    – Harry
    Mar 29, 2019 at 12:22

1 Answer 1

8

Turns out WordPress do not recommend updating GUID thus its disallowed.

But we can update it using sql query:

global $wpdb;
$wpdb->update($wpdb->posts, ['guid' => $newGUID], ['ID' => $postID]);
2
  • I'm sure it would have been intentionally disallowed to prevent changes to it, while it's fine for attachments, preventing changing it in most cases was probably a good idea given it's not supposed to change.
    – majick
    Mar 29, 2019 at 9:43
  • Yes. changing GUID is not wordpress recommends. But sometimes it has/needs to be changed.
    – Harry
    Dec 22, 2019 at 16:21

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.