Support » Fixing WordPress » Custom "Get Users" Query Associated Metadata

  • BH

    I have been working on a complex query to retrieve specific users. Without going into my needs for this particular query, my next challenge was to efficiently retrieve all the usermeta for those users. A JOIN will not work, since the column names are generic and would overwrite each other.

    The solution I reached is to use the user query in a $wpdb->get_cols() to return a list of the IDs, before using it in get_results(). The resulting IDs are then imploded to use in a query [WHERE user_id IN (1,3,5, etc)]. In this way, a single metadata query returns an array of all the metadata.

    The metadata result is then looped through and assembled into a multidimensional array with the user ID used as a key.

    If I understand the Codex correctly (I did not confirm this in the code itself), the double use of the user query shouldn’t cost much as the get_cols caches the entire result and not just the column the function returns.

    (I haven’t done any testing, but I wonder if that means it would be just as efficient or even more efficient to just call the query once as get_results and loop through the results to get the IDs to use in the range for the metadata query… oh well.)

    Any thoughts?

  • The topic ‘Custom "Get Users" Query Associated Metadata’ is closed to new replies.