Even if $object_id
is ignored, it needs to contain a valid value. If you’re deleting all post meta with that key, you don’t need to pass a variable for the ID, a simple integer will suffice.
delete_metadata( $meta_type, 69, $meta_key, $meta_value, true);
Unless you want to delete only specific values, you don’t need $meta_value
either, just pass null
. And $meta_type
will always be ‘post’ in your case, not ‘shop_order’. The meta type is for which table to work on, not which post type. Post type is implicit in the object ID. That just leaves $meta_key
. If there is only one key you are deleting, you don’t even need this variable, you could potentially do something like this:
delete_metadata( 'post', 69, 'my_meta_key', null, true);
You also have some issues with how you are using action hooks. The number of parameters your callback collects must match the number specified in the add_action()
call. You specify 4, but only collect 3. Which parameters are passed depend on the originating do_action()
call, the passed values may not be the ones you need for the related function. You need to look at the originating source code for what is actually passed.
Since a ‘delete_metadata’ action does not exist, you cannot possibly know what parameters are passed. Which brings up the question of what action hook should you use? Unless there is code adding metadata with a direct SQL query, I think ‘added_post_meta’ will work for you. This fires every time add_metadata()
is called for posts. Use of update_metadata()
should be covered by this hook as well since if there is no existing record, add_metadata()
is called.
You really only need to delete the data just added instead of everything because everything else would have been previously deleted on addition as well. You only need to clean up your DB of any existing unwanted meta data one time, this hook will take care of future additions.
See if something like this works for you:
//deletes any post meta data added if key is in the $delete_keys array
add_action( 'added_post_meta', 'del_met', 10, 4 );
function del_met( $mid, $object_id, $meta_key, $_meta_value )
{
$delete_keys = array(
'my_meta_key', 'another_meta_key, 'yet_another',
);
if ( in_array( $meta_key, $delete_keys ) {
delete_metadata( 'post', $object_id, $meta_key );
}
}