$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>$wpdb->update multiple rows, like IN in normal SQL|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

$wpdb->update multiple rows, like IN in normal SQL

matteradmin11PV0评论

I was wondering if it is possible to use $wpdb->update to update values for multiple rows like with IN in "normal" SQL.

I want to accomplish something like this example;

UPDATE [table]
SET [column_1] = [updated_value]
WHERE [column_2] IN ([comma_separated_ids])

I've been searching around for how this would work, but I'm yet to find another question asking this or any kind of answer/blog post explaining whether it can be done or not.

For now I'm using a query instead, but it would be nicer with a simple one liner.

I was wondering if it is possible to use $wpdb->update to update values for multiple rows like with IN in "normal" SQL.

I want to accomplish something like this example;

UPDATE [table]
SET [column_1] = [updated_value]
WHERE [column_2] IN ([comma_separated_ids])

I've been searching around for how this would work, but I'm yet to find another question asking this or any kind of answer/blog post explaining whether it can be done or not.

For now I'm using a query instead, but it would be nicer with a simple one liner.

Share Improve this question edited Dec 27, 2018 at 15:22 Eje 1654 bronze badges asked Aug 1, 2014 at 14:08 Ludvig SjöbeckLudvig Sjöbeck 731 gold badge1 silver badge4 bronze badges 3
  • 1 There is no reason for it not to work. – Mark Kaplun Commented Aug 1, 2014 at 14:18
  • 1 But HOW? I tried adding a comma separated string as value in the where array, but it only updated the first id. – Ludvig Sjöbeck Commented Aug 1, 2014 at 14:54
  • hmm, I see that people with better knowledge then me answered it ;) – Mark Kaplun Commented Aug 1, 2014 at 15:41
Add a comment  | 

1 Answer 1

Reset to default 12

As you can see in source code the = sign is hardcoded in the wpdb::update() method, so, by default, is not possible to use IN for update method.

Simplest way to do the trick is to use the wpdb::query() with your sql query, just be sure to properly escape all values

Example:

function wpdb_update_in( $table, $data, $where, $format = NULL, $where_format = NULL ) {

    global $wpdb;

    $table = esc_sql( $table );

    if( ! is_string( $table ) || ! isset( $wpdb->$table ) ) {
        return FALSE;
    }

    $i          = 0;
    $q          = "UPDATE " . $wpdb->$table . " SET ";
    $format     = array_values( (array) $format );
    $escaped    = array();

    foreach( (array) $data as $key => $value ) {
        $f         = isset( $format[$i] ) && in_array( $format[$i], array( '%s', '%d' ), TRUE ) ? $format[$i] : '%s';
        $escaped[] = esc_sql( $key ) . " = " . $wpdb->prepare( $f, $value );
        $i++;
    }

    $q         .= implode( $escaped, ', ' );
    $where      = (array) $where;
    $where_keys = array_keys( $where );
    $where_val  = (array) array_shift( $where );
    $q         .= " WHERE " . esc_sql( array_shift( $where_keys ) ) . ' IN (';

    if( ! in_array( $where_format, array('%s', '%d'), TRUE ) ) {
        $where_format = '%s';
    }

    $escaped = array();

    foreach( $where_val as $val ) {
        $escaped[] = $wpdb->prepare( $where_format, $val );
    }

    $q .= implode( $escaped, ', ' ) . ')';

    return $wpdb->query( $q );
}

Then use it like so:

wpdb_update_in(
  'posts', // table
  array( 'post_author' => '1', 'post_status' => 'draft' ), // data
  array( 'post_author' => array( '2', '3', '4', '5' ) ), // where
  array( '%d', '%s' ), // format
  '%d' // where format
);

The SQL performed will be

UPDATE wp_posts
SET post_author = 1, post_status = 'draft'
WHERE post_author IN (2, 3, 4, 5)
Post a comment

comment list (0)

  1. No comments so far