0

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 = array(
    '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 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. – Arijit Biswas Mar 29 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 at 11:24
  • I am not changing post permalink. It's for different thing. Anyway I figured it out. – Arijit Biswas Mar 29 at 12:22
0

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, array('guid' => 'your_new_guid'), array('ID' => 45) );

your_new_guid = what you want to update

ID = attachment/post ID needs to be updated

  • 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 at 9:43

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.