[wp-trac] [WordPress Trac] #63420: update_metadata issue when checking $wpdb->update result

WordPress Trac noreply at wordpress.org
Thu May 8 22:17:24 UTC 2025


#63420: update_metadata issue when checking $wpdb->update result
--------------------------------------+-----------------------------
 Reporter:  gdogabriel                |      Owner:  (none)
     Type:  defect (bug)              |     Status:  new
 Priority:  normal                    |  Milestone:  Awaiting Review
Component:  Options, Meta APIs        |    Version:  trunk
 Severity:  normal                    |   Keywords:
  Focuses:  administration, rest-api  |
--------------------------------------+-----------------------------
 Issue found in both trunk and 6.8.

 wp-includes/meta.php > function update_metadata > Line: 314-317

 Incorrect code:
 {{{#!php
 <?php
         $result = $wpdb->update( $table, $data, $where );
         if ( ! $result ) {
                 return false;
         }
 }}}

 Per $wpdb->update documentation, it says:
 > @return int|false The number of rows updated, or false on error

 The code **"! $result"** is **true** when **0** or when **false**.

 It means that when mysql returns "Query executed OK, 0 rows affected.",
 $wpdb->update returns 0, the function update_metadata returns false, and
 it means failure.
 Even when the entry exists, in case the field already has the expected
 value, mysql returns 0 rows affected.

 It’s causing issue in different scenarios, like this one:
 > Updating failed. Could not update the meta value of footnotes in
 database.
 https://wordpress.org/support/topic/updating-failed-could-not-update-the-
 meta-value-of-footnotes-in-database/
 File: wp-includes/rest-api/fields/class-wp-rest-meta-fields.php:404
 {{{#!php
 <?php
                 if ( ! update_metadata( $meta_type, $object_id, wp_slash(
 $meta_key ), wp_slash( $value ) ) ) {
                         return new WP_Error(
                                 'rest_meta_database_error',
                                 /* translators: %s: Custom field key. */
                                 sprintf( __( 'Could not update the meta
 value of %s in database.' ), $meta_key ),
                                 array(
                                         'key'    => $name,
                                         'status' =>
 WP_Http::INTERNAL_SERVER_ERROR,
                                 )
                         );
                 }
 }}}



 For fixing, I suggest using the identity operator to compare the $result
 to false:
 wp-includes/meta.php > function update_metadata > Line: 314-317
 {{{#!php
 <?php
         $result = $wpdb->update( $table, $data, $where );
         if ( false === $result ) {
                 return false;
         }
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/63420>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list