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? Commented 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. Commented 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. Commented Mar 29, 2019 at 11:24
  • I am not changing post permalink. It's for different thing. Anyway I figured it out. Commented 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. Commented Mar 29, 2019 at 9:43
  • Yes. changing GUID is not wordpress recommends. But sometimes it has/needs to be changed. Commented Dec 22, 2019 at 16:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.