I’m using metadata in pages to store variables and do server side caching for dynamic generated PayPal buttons and other variables about the pages (page specific).
I’m trying to hook the page meta data’s update, add and delete functions. The update and add works fine (both with same function) but I can’t figure delete out.
In order to use delete_post_data I need the post_id
but the do_action
doesn’t return that (at least what I can figure) from WordPress’s source code it seems to return this:
do_action( 'delete_postmeta', $postmetaids ); //sometimes $post_meta_ids
where
$postmetaids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE meta_key = %s", $post_meta_key ) );
or
$post_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $postid ));
The code I’m using to call the hooks:
add_action ('update_postmeta', 'wp_paypal_meta_change',1,4);
add_action ('add_postmeta', 'wp_paypal_meta_change', 1, 4);
add_action ('delete_postmeta','wp_paypal_meta_delete', 1, 2);
The delete function:
function wp_paypal_meta_delete($post_id, $meta_keys){
/*
handles cases where the user intentionally deletes the metadata, and deletes
the appropriate hidden metadata variable in order to make sure the cached
PayPal form matches the product data.
*/
if ($meta_keys == 'image'){
delete_post_meta($post_id,'_thumb');
} else if ($meta_keys == 'shipping' || $meta_keys == 'cost' || $meta_keys== 'bookname'){
delete_post_meta($post_id,'_paypal');
}
}
I know the $post_id
doesn’t get into the function, but it’s there because I hoped WordPress somehow references it – didn’t work.
From the OPs edit moved to an answer.