WordPress get_user_meta() – is there a better way to pull bulk user data?

I am working on a custom report of member data for a WordPress site. Some of the fields I need to include in the report are stored in the wp_usermeta table, but when I iterate through each member to retrieve that data, it eventually times out.

I’m currently using get_user_meta() because it seemed faster than doing the queries manually, but it still times out. There are about 5,600 users to process. It gets through 4000 of them just fine, but if I try to run it on the full set, it inevitably times out. Is there a better way to do this? Is there a way to pull bulk data from the wp_usermeta table instead of doing it separately for each user? I thought of maybe trying to pull down the entire usermeta table in one query and using PHP to manipulate it, but there are over 540,000 rows in that table…

Read More

Here’s where I’m at so far…

 require_once( "db_connection.php" );
 require_once( "../wp-config.php" );
 $q = "SELECT ID,user_login,user_email,last_login,display_name FROM wp_users WHERE user_email='' ORDER BY user_login ASC;";
 $r = mysql_query( $q );
 $members = array();
 while ( $member = mysql_fetch_array( $r ) ) {
      $usermeta = get_user_meta($member['ID']);
      $members[$member['ID']] = $member;
      $members[$member['ID']]['meta'] = $usermeta;
      }

 echo "<pre>n";
 var_dump( $members );
 echo "</pre>n";

Thanks so much for any help/advice!

Related posts

Leave a Reply

1 comment

  1. Well, I tried the “pull down everything” approach and it seems to work without timing out. Here’s my modified approach:

     require_once( "db_connection.php" );
     require_once( "../wp-config.php" );
     $q = "SELECT ID,user_login,last_login,display_name FROM wp_users WHERE user_email='' ORDER BY user_login ASC;";
     $r = mysql_query( $q );
     $members = array();
     while ( $member = mysql_fetch_array( $r ) ) {
          $members[$member['ID']] = $member;
          }
    
     $q = "SELECT * FROM wp_usermeta WHERE (meta_key='first_name' OR meta_key='_slt_middle_initial' OR meta_key='last_name' OR meta_key='_slt_email2' OR meta_key='_slt_email3');";
     $r = mysql_query($q);
     while ( $usermeta = mysql_fetch_array( $r ) ) {
          if ( $members[$usermeta['user_id']]['ID'] != "" ) {
               $members[$usermeta['user_id']][$usermeta['meta_key']] = $usermeta['meta_value'];
               }
          }
    
     echo "<pre>n";
     var_dump( $members );
     echo "</pre>n";
    

    It’s too bad get_user_meta() doesn’t work for bulk data – it would be nice if we could pass it an array of IDs and fields we want, for example, and get a multidimensional array back… but I guess this way works for now, it’s just a little cumbersome.