Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

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:

<?php
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
         "
        ,$meta_ids
    ) );

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

    if ( $meta_keys )
        return $meta_keys; 

    // else:
    return false;
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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