Support

Account

Home Forums ACF PRO Get Label where value equals \"X\" – direct DB query

Unread

Get Label where value equals \"X\" – direct DB query

  • Hi Guys,

    I’m doing something a little different than a normal “echo $field[label]”. I’m mixing regular database calls with acf values/labels.

    We have a separate table in the database which stores chapter information for an organization.
    We have fields on the User account which tell us what chapter a user is in.

    No problems.

    We update the table with a record assigned to a chapter and this stores the “value” from the ACF select (Chapter Name : chapter1) — so “chapter1” gets saved in the chapter column of the table. Perfect – no problems!

    We’re now looping through the users to retrieve information but it will only pull the value (as that’s what’s stored in the column table)

    $user = get_user_by( 'id', $results->org_user_id );
    <?php echo $results->chapter; ?>

    Since we are querying the database table directly, and that returns, what is essentially a value, is there a way that we can then compare the value against the ACF Field to retrieve the label that corresponds to the value?

    Essentially, we’d get the chaptername from the database ‘chapter1’, then a query that looks up the field_xxxx[label] where $chaptername = chapter1.

    The “code” below is for explanation only, obviously it would never be this easy 😉

    $field1 = get_field_object('field_aa11bb22cc33dd33');
    $field2= $results->chapter;
    return $field1[label] where $field2 == $field1[value]

    Any thoughts on a starting point?

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.