Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.