Support

Account

Home Forums General Issues Querying users by custom field

Helping

Querying users by custom field

  • I have created a custom checkbox field that is assigned to users. I would like to know if there is a way to query for users that have a certain value on these fields using WP_User_Query.

    I realize that I can query for all users and then loop through them and only display the ones that have that value using get_field() but that seems a little inefficient if the user base grows.

  • Yes there is. ACF stores fields on user forms in the usermeta table.

    The difficulty is that checkbox fields are arrays stored as serialized data. In order to do a user query you would need to use the “LIKE” compare method in the meta_query for the user query.

    something like

    
    'meta_query' => array(
      array(
        'key' => 'my_field',
        'value' => 'my_value',
        'compare' => 'LIKE'
      )
    )
    

    if you’re looking for more than one value then you’d need to look for each value with and ‘AND’ or ‘OR’ for the relation value, depending on what you want returned.

    https://codex.wordpress.org/Class_Reference/WP_User_Query

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.