Advanced Custom Fields
[resolved] Custom field for a user, then fetching that user back (4 posts)

  1. Jason Judge
    Posted 2 years ago #

    I've tried searching for the answer to this, but just cannot find any description of examples of what I am trying to do.

    I would like to link WP users to an external CRM. To provide the link, I have created a custom field against users called "External ID". How that external ID gets into that field is not the issue here; finding a user with a given ID is.

    So, during login, a user is authenticated and that authentication process gives me the External ID. Now I want to use that External ID to find the WP user (if one exists) that has that ID in the custom field.

    I am sure it is just a couple of lines of code, but I just can't see how to do it. Any hints on how I would do this?

    In summary: users have a custom field, each with a unique ID string in. Given an ID, in code I would like to fetch the WP_User object for the user that matches that ID in its custom field.


  2. Jason Judge
    Posted 2 years ago #

    If I were using a standard meta field, rather than an ACF, I would do this:

    $existingUsers = get_users(
        array('meta_key' => 'external_id', 'meta_value' => $my_id)
    // There should be zero or one user returned, so long as the external_id metafields have been managed properly.
    if (count($existingUsers) == 1) $my_user = reset($existingUsers); // All happy :-)

    I'm looking for the AVF equivalent to this.

  3. Jason Judge
    Posted 2 years ago #

    Well, the answer is simple, and right in front of eyes.

    ACF will make any field created against a user available as user metadata. So if I have created an ACF field name *name* external_id, then I can fetch all users with a given external ID using the exact code in my previous post.

    What I overlooked, in trying to find an ACF API to do the searching for me, is that ACF hooks nicely into the standard WP metadata and functionality, so I can use standard WP ways to fetch data outside of and independent to "the loop".

  4. Jason Judge
    Posted 2 years ago #

    I have a way forward, so can close this now.

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Advanced Custom Fields
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic


No tags yet.