Using API 4 (php), how do I retreive a custom field value for a specific contact, using the custom field id instead of the custom field name.

This works (for contact with id=1234), but requires the use of the custom field name (Extra_Info.My_Custom_Field_Name), but I want to use the custom field id of say 37:

$result = civicrm_api4('Contact', 'get', [ 
  'checkPermissions' => FALSE, 
  'select' => [ 'Extra_Info.My_Custom_Field_Name', ], 
  'where' => [ ['id', '=', '1234'], ], 
  'limit' => 1,
  • I'm curious why you want to do that? One of the benefits of APIv4 vs APIv3 is that you don't need to mess around with those field numbers.
    – Aidan
    Commented Feb 17, 2023 at 8:38
  • @aidan Mostly because that is how I was trying to do it originally and could not figure out how to do it that way... so now I just want to know how!
    – jsherk
    Commented Feb 17, 2023 at 12:52
  • 1
    Ah! I don't that you can do that directly. You could use CustomField::get()->addSelect('name', 'custom_group_id:name')->addWhere('id', '=', 37) to get the field and group names to plug into your existing query.
    – Aidan
    Commented Feb 20, 2023 at 8:54


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.