Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kellenmace/edbf3845f5e52fd3464a36e2318bfea4 to your computer and use it in GitHub Desktop.
Save kellenmace/edbf3845f5e52fd3464a36e2318bfea4 to your computer and use it in GitHub Desktop.
WordPress User Query Not Working with AJAX/WP-Cron
<?php
private function get_foremen_in_division( $division ) {
$original_user_id = get_current_user_id();
// Set current user to be a Super Admin so the user query below works during the AJAX requests & WP-Cron jobs.
$super_admins = get_super_admins();
if ( $super_admins ) {
$super_admins = array_values( $super_admins );
wp_set_current_user( null, $super_admins[0] );
}
$foremen = get_users( [
'role' => 'foreman',
'fields' => [ 'ID', 'display_name' ],
'meta_key' => '_division',
'meta_value' => $division,
'count_total' => false,
] );
// Restore original user.
wp_set_current_user( $original_user_id );
return $foremen;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment