WordPress User Query Not Working with AJAX/WP-Cron
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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