I was wondering if anyone has tried doing a query of users that appear in a user field on a post and then count how many times each user appears in that field and update a field on their profile with that count, here is what I have so far, any ideas on how I can get this working would be much appreciated.
add_action( 'run_snippet_hourly', function () {
$pds_project_manager_count = 0;
$user_query = new WP_User_Query( array( 'role' => 'um_pds-project-manager' ) );
if ( ! empty( $user_query->results ) ) {
foreach ( $user_query->results as $user )
$args = array(
'post_type' => 'project',
'meta_key' => 'pds_project_manager', 'meta_value' => $user->ID
);
$query = new WP_Query( $args );
$pds_project_manager_count += $query->post_count;
// Update user profile field user_project_count with the count
update_user_meta( $user->ID, 'user_project_count, $pds_project_manager_count);
}
} );//end Cron
ACF stores user fields as serialized arrays of user IDs in string format.
To query by a user field you must use the LIKE
comparison on the ID you are looking for enclosed in double quotes '"'.$user_id.'"'