Question

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?

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top