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 need to change the meta value of all users at once. Seem there are no api for this,except using custom database query.

This is my code:

$matches = array('meta_key' => 'user_token');
$data = array('meta_value' => '100');
$wpdb->update( "$wpdb->usermeta", $data, $matches);

The problem is this only update one user meta value, not all users meta value with the meta key of 'user_token'.
Does $wpdb->update limited to update single row only? Or I did something wrong?

share|improve this question
    
The original code works for me, that is, it updates multiple rows at a time. But I made sure to declare the global $wpdb before the code. –  Joel Feb 16 at 13:08
add comment

2 Answers

Your code of wpdb method is correct. posible reasons why it fails...

  1. you have only one user that match's meta_key = 'user_token'
  2. other functionality change query at query hook filter strage.

debug the final SQL with

add_filter( 'query', 'query_report', 10000 );
function query_report($sql){ 
    var_dump($sql); 
    return $sql;
}
share|improve this answer
    
I think I found the problem, SQL result cache is the case. Because I was looking directly to the database. I keep refreshing the database while the database not ready to perform the query. –  dev-jim Nov 14 '12 at 18:07
add comment

Maybe there are different ways for UPDATE:

1)

<?php
define('WP_USE_THEMES', false);
require( dirname( __FILE__ ) . '/wp-blog-header.php' );
global $wpdb;

$matches = array('meta_key' => 'user_token');
$data = array('meta_value' => '100');
$wpdb->update( "$wpdb->usermeta", $data, $matches);
?>

2)

<?php
define('WP_USE_THEMES', false);
require( dirname( __FILE__ ) . '/wp-blog-header.php' );
global $wpdb;

$multi=array('34','55');
Foreach ($multi as $each){
  $result = $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->postmeta SET meta_value = '111' WHERE post_id = '$each'", "blahblah" ) ); var_dump($result);
  //OR update_post_meta($each, 'meta_key_NAME', 'YOUR_VALUE' );
  }
?>
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.