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


$postmetaids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE meta_key = %s", $post_meta_key ) );


$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'){
    } else if ($meta_keys == 'shipping' || $meta_keys == 'cost' || $meta_keys== 'bookname'){


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.

1 Answer 1


From the OPs edit moved to an answer.

I found an answer, also not a straight path, I'm sure there's a better way to do this. basically I use a little helper method that queries the SQL with the meta_id and return the post_id:

function get_key_by_id( $meta_ids )
    global $wpdb;

    $meta_ids = absint( $meta_ids );

    // Debugging:
    // $wpdb->show_errors();

    $meta_keys = $wpdb->get_var( $wpdb->prepare(
            SELECT meta_key 
            FROM {$wpdb->postmeta}
            WHERE meta_id = %d
    ) );

    // Debugging:
    // $wpdb->print_error();

    if ( $meta_keys )
        return $meta_keys; 

    // else:
    return false;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.