I have put together the below snippet to count how many time seach user in the specified role appears in an ACF user field on several of my posts and it works fine if I actually specify someone's user_id (eg: $user_id= 32;), but when I want to run it for all of the users in that role ($user_id= $user->ID;), it doesn't update each user field with their count. Any ideas would be appreciated.
add_action( 'run_snippet_hourly', function () {
$user_query = array(
'role' => 'um_pds-project-manager',
'orderby' => 'display_name',
'order' => 'ASC'
);
$users = get_users( $user_query );
if ( !empty( $users ) ) {
foreach ( $users as $user )
$user_id= $user->ID;
$args = array(
'posts_per_page' => -1,
'post_type' => 'project',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'status',
'value' => '1'
),
array(
'key' => 'pds_project_manager',
'value' => $user_id,
'compare' => 'LIKE'
)
)
);
$posts_pm = get_posts($args);
$pds_project_manager_count = count($posts_pm);
// Update user profile field user_project_count with the count
update_user_meta( $user_id, 'user_project_count', $pds_project_manager_count);
}
} );//end Cron