1

delete_post_meta seems to merely clear out the value for a given post meta key. It doesn't really completely delete the row. I've been digging a lot, but I can't seem to find any function out there that deletes the post meta entry completely.

Please, could anyone point me in the right direction? Do I have to use $wpdb to delete the row fully or is there a tucked away function to do this for me?

Edit:

The following has been tested and it's working, but it's using $wpdb. Is this the proper way to do it?

$wpdb->delete( $wpdb->postmeta, array( 'post_id' => 744, 'meta_key' => 'referrers' ), array( '%d', '%s' ) );

Edit2:

Here's my old code:

// $post_id contains an integer value
// $referrers contains an array
$referrers = get_post_meta( $post_id, 'referrers', true );
for ( $i = 0; $i < count( $referrers ); $i++ ) {
    delete_post_meta( $post_id, "referrers_{$i}_contact_id" );
    delete_post_meta( $post_id, "_referrers_{$i}_contact_id" );
    delete_post_meta( $post_id, "referrers_{$i}_rule" );
    delete_post_meta( $post_id, "_referrers_{$i}_rule" );
    delete_post_meta( $post_id, "referrers_{$i}_amount" );
    delete_post_meta( $post_id, "_referrers_{$i}_amount" );
}

The above works, but it merely clears out the values. Am I using is incorrectly? Have I missed something by accident?

1 Answer 1

3

delete_post_meta will delete the meta row. delete_post_meta also uses wpdb class to delete the meta rows. Can you show us the full code.

For your information delete_post_meta executes the following mysql command

DELETE FROM wp_postmeta WHERE meta_id IN( "85" );

through delete_metadata function. meta_id 85 is just given for example purpose. It is dynamic. so you don't need to run your own mysql queries in order to avoid bugs and security risks.

NOTE: I don't have enough reputation to comment, so giving this message as answer.

5
  • Thank you WPTC! I've appended my other code above for you to have a look. God I hope it isn't some stupidly small thing :(
    – adamj
    Nov 5, 2015 at 2:54
  • Never mind, it was a stupid mistake of mine. I swear, starring at stackexchange/overflow for like 5min lets you find mistakes instantly... that and right after posting a question you end up finding a solution. /shrug such is life.
    – adamj
    Nov 5, 2015 at 3:10
  • Do you wanted to delete referrers or referrers_*contact_id etc. Bcoz in the query you were deleting referrers and in the function you are not deleting referrers
    – WPTC-Troop
    Nov 5, 2015 at 3:10
  • @adamj cool you found out the mistake. May I know what mistake you did?
    – WPTC-Troop
    Nov 5, 2015 at 3:14
  • It was ridiculously stupid, but sadly it wasn't something that popped up immediately as it was done via AJAX. It didn't send across the $post_id to the above deletion code.
    – adamj
    Nov 5, 2015 at 3:34

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.