Make WordPress Core

Ticket #9009: 9009-enhanced-2.diff

File 9009-enhanced-2.diff, 1.4 KB (added by Denis-de-Bernardy, 16 years ago)
  • Users/denis/Sites/sem-pro/wp-includes/post.php

     
    663663 * @uses $wpdb
    664664 *
    665665 * @param string $post_meta_key Key to search for when deleting.
     666 * @param string $operator The SQL operator. Can be any of LIKE, NOT LIKE, REGEXP, NOT REGEXP
    666667 * @return bool Whether the post meta key was deleted from the database
    667668 */
    668 function delete_post_meta_by_key($post_meta_key) {
     669function delete_post_meta_by_key($post_meta_key, $operator = false) {
    669670        global $wpdb;
    670         if ( $wpdb->query($wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE meta_key = %s", $post_meta_key)) ) {
    671                 /** @todo Get post_ids and delete cache */
    672                 // wp_cache_delete($post_id, 'post_meta');
     671       
     672        switch ( $operator ) {
     673        case 'LIKE':
     674        case 'NOT LIKE':
     675        case 'REGEXP':
     676        case 'NOT REGEXP':
     677                break;
     678        default:
     679                $operator = 'LIKE';
     680        }
     681       
     682        $post_ids = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT post_id FROM $wpdb->postmeta WHERE meta_key LIKE %s", $post_meta_key));
     683        if ( $post_ids ) {
     684                $wpdb->query($wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE meta_key LIKE %s", $post_meta_key));
     685                foreach ( $post_ids as $post_id )
     686                        wp_cache_delete($post_id, 'post_meta');
    673687                return true;
    674688        }
    675689        return false;