Skip to content

Instantly share code, notes, and snippets.

@montchr
Last active August 29, 2015 14:01
Show Gist options
  • Save montchr/484f4dff49eac240fba8 to your computer and use it in GitHub Desktop.
Save montchr/484f4dff49eac240fba8 to your computer and use it in GitHub Desktop.
Get a list of WordPress usernames and the number of posts attributed to them on the current site.

Export Users (Live WPEngine Site)

  1. Go to the specific site's Dashboard and click Users → Export User Data.
  • Select the following user meta fields to export:
    • Description
    • First name
    • Last name
    • LDAP login
    • Nickname
  • Change format to CSV.
  • Export and download the resulting CSV file.

Create Spreadsheet

  1. Go to the "SMC - Web Team" folder in Google Drive and open the spreadsheet named "Migration User Data."
  • In the spreadsheet viewer, go to File → Import, click "Upload" and upload your CSV file.
  • For "Import action," select "Insert new sheet(s)". Click "Import". The resulting name should suffice.
  • Add a migrate_num_posts column
  • Select the header row (should be first) and go to View → Freeze Rows → Freeze 1 row.

User Post Numbers (Staging WPEngine Site)

  1. Sort spreadsheet by the ID column
  • Make sure you're working off of a development or staging site.
  • If you see the "Debug" button in the admin bar, skip to step 6.
  • Install and Network Activate the Debug Bar and Debug Bar Console plugins.
  • Go to the site you wish to get user post number info for.
  • Click the "Debug" button in the top right corner of the admin bar.
  • Click "Console" in the Debug interface.
  • Paste the contents of user-post-nums.php in the PHP input field.
  • Run by clicking "Run" in the upper right of the interface or just press Shift + Enter
  • Copy the second set of results (just the numbers)...
  • ...double check and make sure the sheet is sorted by the ID column!...
  • ...and paste it into the migrate_num_posts column in your spreadsheet.
  • Make sure to double check the accuracy!

Remove Unnecessary Information

  1. In the sheet you just modified, sort by the migrate_num_posts column.
  • Remove all the rows with a migrate_num_posts value of 0
  • Remove the row corresponding to the admin user.
  • Remove the ID, migrate_num_posts, and user_registered columns.

Import LDAP Users (Live Rackspace Site)

  1. Sort rows by the ldap_login column
  • Select and copy all the values from the user_login column in the rows where ldap_login equals TRUE
  • On the Live Rackspace site, go to the Dashboard for the site you're working on.
  • Go to Users → Add User.
  • Paste the user_login values into the "Add Bulk Users" field. They should already be separated by newlines.
  • Make sure each will be added as a Subscriber.
  • Submit by clicking the "Add Bulk Users" button.
  • Remove the ldap_login column from the spreadsheet.

Cleaning up the Spreadsheet

  1. After submitting the list of LDAP users, it's likely that some will not be found in the directory. You'll be notified about which users didn't make it. Don't navigate away from this page!
  • Update the "Master SMC WP Network User List" accordingly
    • LDAP? equals y
    • Added equals y
  • In your spreadsheet, select all the rows corresponding to the users that were successfully added.
  • Press delete. The values in the rows will be deleted but the empty rows will remain.
  • Sort any column so that all the empty rows appear grouped together at the top or bottom of the sheet. Select them all and go to Edit → Delete rows… (where the range of rows will appear at the end of the option)
  • Remove the ldap_login column.
  • Only the non-LDAP users and the users that were't found in the LDAP directory should remain in the spreadsheet.

Import Remaining Non-LDAP Users (Live Rackspace Site)

  1. In the spreadsheet: File → Download as → Comma-separated values (.csv, current sheet)
  • In the site Dashboard, go to Users → Import from CSV.
  • Upload the CSV file you just created.
  • Check the "Password nag" box.
  • Import.
  • Profit!
<?php
$user_args = array('fields' => 'id');
$user_ids = get_users($user_args);
$user_post_nums = count_many_users_posts($user_ids);
// Sort by user ID
ksort($user_post_nums);
echo '<br><br>';
// Echo the usernames and their associated number of posts in order of the user ID
foreach ($user_post_nums as $id => $post_num) {
$user_data = get_userdata($id);
$user_name = $user_data->user_login;
echo $user_name . ': ' . $post_num . '<br>';
}
echo '<br><br>';
// Echo just the number of posts per user in order of
// the user ID (but without the username!).
//
// This makes it easy to copy and paste into a spreadsheet,
// but make sure to double check the accuracy by referencing
// the first set of info with usernames. There's no guarantee
// that this info will match up (but it's pretty likely).
foreach ($user_post_nums as $id => $post_num) {
echo $post_num . '<br>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment