This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

WP_User_Query to list all Wordpress users with usermeta

By: jcarroll_massagenet on Apr 17th, 2012  |  syntax: PHP  |  size: 1.33 KB  |  views: 503  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. /*
  3. Template Name: MassageNetUsers
  4. Wordpress Template which displays all users in HTML table
  5. */
  6. ?>
  7.  
  8. <?php get_header();
  9.  
  10. // Get all users
  11. $wp_user_search  = new WP_User_Query( array( /*'role' => 'subscriber', */'fields' => 'all_with_meta' ) );
  12. $users = $wp_user_search->get_results();
  13.  
  14. // Custom fields stored in wp_usermeta table
  15. $massagenet_user_fields = array(
  16.                 'ID',
  17.                 'research_id',
  18.                 'massage_therapist',
  19.                 'massage_researcher',
  20.                 'massage_educator',
  21.                 'massage_student',
  22.                 'user_type_other',
  23.                 'user_type_other_text',
  24.                 'title',
  25.                 'first_name',
  26.                 'middle_initial',
  27.                 'last_name',
  28.                 'suffix',
  29.                 'city',
  30.                 'postal_code',
  31.                 'state_province',
  32.                 'country',
  33.                 'graduation_date',
  34.                 'licensed',
  35.                 'practice_state',
  36.                 'license_expiration',
  37.                 'wp-last-login',
  38.                 'referral_source'
  39.         );
  40.  
  41. // Create HTML table to display all users
  42. function display_all_metauser_fields() {
  43.         global $massagenet_user_fields;
  44.         global $users;
  45.        
  46.         echo "<table border='1'><tr>";
  47.         foreach ($massagenet_user_fields as $field) {
  48.                 echo "<th>".$field."</th>";
  49.         }
  50.         echo "</tr>";
  51.         foreach ( $users as $user )
  52.         {
  53.                 echo "<tr>";
  54.                 foreach ($massagenet_user_fields as $field) {
  55.                         echo "<td>"; echo $user->get( $field ); echo "</td>";
  56.                 }
  57.                 echo "</tr>";
  58.         }
  59.         echo "</table>";
  60.  
  61. display_all_metauser_fields();
  62.  
  63. }
  64.  
  65. get_footer();
  66. ?>
clone this paste RAW Paste Data