skip to Main Content

How would I change the following function so that $user->ID would return all users ID’s one at a time?

It now only returns the first user with lowest ID, and then adds that ID to all following users.
Resulting in that all users get the same categories as the first user.

user_kategoriat is an ACF field: here’s what I’ve been looking at:
https://www.advancedcustomfields.com/resources/how-to-get-values-from-a-user/

add_filter( 'algolia_user_record', 'my_post_attributes', 10, 2 );

function my_post_attributes( $attributes ) {
    
    $users = get_users( array( 'fields' => array( 'ID' ) ) );
        
    foreach ( $users as $user ) { 
                $ID = $user->ID;
        $attributes['kategoriat'] = get_field('user_kategoriat', 'user_'. $ID );
        return $attributes; 
    }
        
}

2

Answers


  1. Chosen as BEST ANSWER

    Here is what worked:

    add_filter( 'algolia_user_record', 'my_post_attributes', 10, 2 );
    function my_post_attributes( array $attributes, $user ) {
    
    $users = get_users( array( 'fields' => array( 'ID' ) ) );
    $cats = array( get_field('user_kategoriat', 'user_'. $user->ID ) );
    
    foreach ( $users as $user ) {
    $user = $attributes['kategoriat'] = $cats;
    }
    return $attributes;
    }
    

  2. return exits the loop. You can try this:

    $users = get_users( array( 'fields' => array( 'ID' ) ) );
    $attributes = array();
        
    foreach ( $users as $user ) { 
        $attributes['kategoriat'] = array(get_field('user_kategoriat', 'user_'. $user->ID ));
    }
    return $attributes; 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search