Joy
(@joyously)
You don’t need to unserialize it to delete it.
Try one of the Bulk Delete plugins.
I´ve visited your link and didn´t find any plugin related with images metadata, even the pros versions (just attachments as a whole, not differentiating arrays).
Do you know why doing it manually is not recommended in WP db?
Joy
(@joyously)
The plugins I was thinking of just delete things that match what you choose, like posts or pages or post meta. I guess you are wanting to keep the post meta and just delete some parts of it.
If you know how to read the serialized data, you can do it manually. It’s a hassle because of getting the lengths correct. Or you can write a small plugin to get the attachment data, delete the image_meta key, and save it. WP does the unserializing and serializing for you.
https://developer.wordpress.org/reference/functions/wp_get_attachment_metadata/
https://developer.wordpress.org/reference/functions/wp_update_attachment_metadata/
There is also
https://developer.wordpress.org/reference/functions/wp_restore_image/
if you change your mind.
@joyously,
Yes, I´ve seen the function. But I don´t know a safe query to run in db for already stored metadata. If you read my first post in this topic, there´s a filter but previous data is already serialized.
You don’t need to know, or care, what the metadata is to delete it.
Obviously make a backup first.
Next, take a look at the postmeta table. You will find that it consists of data stored by meta_key and meta_value. You only want to delete the data with the meta_key = ‘_wp_attachment_metadata’. What the value is, well, you kinda don’t care if you’re just deleting it.
Now, if you want to read the data and save it somewhere else first, by reformatting it in some manner, then the function you want is wp_get_attachment_metadata
. And it doesn’t return serialized data to you, it returns an array of the data. Already deserialized. This is true of serialized data in general, if you use the WordPress built-in functions, then it handles that serialization stuff for you both in saving and in retrieving the data.
In other words, don’t run an SQL query yourself, instead write some PHP code to loop through the attachment posts to get and reformat and resave your data in the way you want it, then run it in the WordPress context, as in a custom made plugin.
@otto42,
I don´t want to delete the entire meta_value, just part of it as @joyously understood. Is there a function to extract the key array ([image_meta]? I´ve found this:
`function maybe_unserialize( $data ) {
if ( is_serialized( $data ) ) {
return @unserialize( trim( $data ) );
}
return $data;
}`
What do you think?
Joy
(@joyously)
When you retrieve the attachment meta data with the WP function, WP deserializes it for you. You then use normal array manipulation on the data like
unset( $data['image_data'] );
and then use the other WP function to write that back to the database.
Hello @joyously,
Thanks for your response and sorry about my delay.
Could you provide this function syntaxis? For unserializing, then unsetting meta data and then writing it back to db?
Regards,
Julia