I have a custom post type where the user can set a video ‘url’ custom field. I need to download meta-data about the video (e.g. title, description, thumbnail) and store it along with the rest of the post’s data.
Since downloading this data can be a bit long, I’d like to do this only when it’s necessary, i.e. when the ‘url’ field has changed.
Is there a way to check whether a field’s value has changed on save_post
?
Usually there is no way to check if the meta data is updated or not, but you can use updated_postmeta
and added_post_meta
to get kinda similar functionality. Please have a look at the below code-
add_action( 'updated_postmeta', 'the_dramatist_updated_postmeta', 10, 4 );
add_action( 'added_post_meta', 'the_dramatist_updated_postmeta', 10, 4 );
function the_dramatist_updated_postmeta(
$meta_id,
$object_id,
$meta_key,
$meta_value
) {
// check meta key with $meta_key parameter
if ( 'your_meta_key' !== $meta_key ) {
return;
}
// better check with the exiting data with the $meta_value to check if the data is updated or not.
// then do what you want to do with the data
}
Hope this above helps.