Support

Account

Home Forums General Issues custom API ROUTE user count

Solved

custom API ROUTE user count

  • Hi,

    I am trying to make a custom API route that displays the number of users, so that I can use it on another website.

    Code:

    function getUserAmount() {
        $users = get_users(); // Get all wordpress users
       
        if ( empty( $users ) ) { // If there aren't any users display an error
          return 'There aren\'t any users to display.';
        }
        return count($users); // Return amount of users
    }
    /* Preparing to serve an API request */
    add_action( 'rest_api_init', function () {
        register_rest_route( '/wp/v2', '/users', 
            array(
                'methods' => 'GET',
                'callback' => 'getUserAmount',
            )
        );
    } );

    Now, the problem is that I am using a custom field that determines if a user is ‘inactive’, and not displaying on the ‘our-people’ page if they are.
    The API request counts ALL users, and ignores the custom field.

    Is there any way that I can fix this?

    Thanks!

  • if(!get_field('inactive', 'user_' . $user->ID))

    ^How it works on the ‘our-people’ page.
    Is it maybe possible to process this if statement into the API request?

  • 
    $users = get_users(); // Get all wordpress users
    $active_users = array();
    foreach ($users as $user) {
      if (!get_field('inactive', 'user_' . $user->ID)) {
        $active_users[] = $user;
      }
    }
    
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘custom API ROUTE user count’ is closed to new replies.