-1

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 
Webbb
  • 31
  • 7
  • _"Any ideas would be appreciated."_ - so would be an explanation of what actually happens, and what debugging measures you have taken so far, and with what result. – CBroe Jan 31 '23 at 08:16
  • `'compare' => 'LIKE'` - why, on a user ID comparison? It does not _hurt_ since there are no placeholders involved, but for clarity the normal equality operator `=` would be preferable IMHO. – CBroe Jan 31 '23 at 08:18
  • The ACF user field is a user object and per ACF they state you need to use LIKE for this type of query. That portion works without issue, and measuring the performance it’s not too bad especially running it only hourly or only daily. I am just having issues getting it to actually update each users profile with their count. – Webbb Feb 01 '23 at 04:05

1 Answers1

0

I was able to get this to work, here is my revised snippet, I modified it to try and clarify what someone would need to change to work for them. If you use an ACF user field in your posts, this can get users by a role and then count how many times each user appears in the user field on all of your posts and then updates a field on their profile with the count.

add_action( 'run_snippet_hourly', function () {// I am triggering this with a cron job

// Get the user ID's for all users in a specific role.
$user_ids = get_users( array('role' => 'YOUR ROLE HERE', 'fields' => 'ID' ) );

// Loop through each user and get the count of posts that have the user in the ACF user field.
foreach ( $user_ids as $user_id ) {

    // Get the ACF user field for all posts.
    $args = array(
        'post_type'   => 'YOUR POST TYPE HERE',
        'posts_per_page' => -1,
        'meta_query'  => array(
            'relation' => 'AND',
            array(
                'key'     => 'YOUR FIELD KEY HERE',
                'value'   => '"' . $user_id . '"',
                'compare' => 'LIKE'
            )
        )
    );

    $query = new WP_Query( $args );
    $count = $query->post_count;

    // Update the user meta field user_post_count with the count for each user. Use ACF to add a text field to all users profiles.
    update_user_meta( $user_id, 'user_post_count', $count );

}
    
} );//end Cron 

Webbb
  • 31
  • 7