I'm trying to update GUID on a attachment. But somehow it's not working neither it's throwing any error.

Example Code:

$my_post = [
    'ID'   => 45,
    'guid' => 'https://example.com/foo.png'

wp_update_post( $my_post );

WordPress does not seem to update the GUID.

But if we try update other values such as post_parent, it works fine.

Why is that?

  • Why need to update guid? – birgire Mar 29 '19 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 '19 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. – Jacob Peattie Mar 29 '19 at 11:24
  • I am not changing post permalink. It's for different thing. Anyway I figured it out. – Harry Mar 29 '19 at 12:22

I tried a lot of things but somehow WordPress won't let me update GUID using wp_update_post() function.

But we can update GUID using SQL query. The best approach would be:

global $wpdb;
$wpdb->update($wpdb->posts, ['guid' => 'your_new_guid'], ['ID' => 45]);

your_new_guid = what you want to update

ID = attachment/post ID needs to be updated

| improve this answer | |
  • 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 '19 at 9:43
  • Yes. changing GUID is not wordpress recommends. But sometimes it has/needs to be changed. – Harry Dec 22 '19 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.