Récupérer la field key ACF à partir du field name

event 07 Juil 2017 account_circle Julien
label_outline PHP WordPress

Notamment pour un get_field_object($selector, [$post_id], [$format_value], [$load_value]) pour lequel la doc ACF précise que $selector (string) (Required) The field name or field key, alors que seule la field key fonctionne.

[pastacode lang= »php » manual= »%3C%3Fphp%0A%09%20%0Afunction%20acf_get_field_key(%20%24field_name%2C%20%24post_id%20)%20%7B%0A%09global%20%24wpdb%3B%0A%09%24acf_fields%20%3D%20%24wpdb-%3Eget_results(%20%24wpdb-%3Eprepare(%20%22SELECT%20ID%2Cpost_parent%2Cpost_name%20FROM%20%24wpdb-%3Eposts%20WHERE%20post_excerpt%3D%25s%20AND%20post_type%3D%25s%22%20%2C%20%24field_name%20%2C%20’acf-field’%20)%20)%3B%0A%09%2F%2F%20get%20all%20fields%20with%20that%20name.%0A%09switch%20(%20count(%20%24acf_fields%20)%20)%20%7B%0A%09%09case%200%3A%20%2F%2F%20no%20such%20field%0A%09%09%09return%20false%3B%0A%09%09case%201%3A%20%2F%2F%20just%20one%20result.%20%0A%09%09%09return%20%24acf_fields%5B0%5D-%3Epost_name%3B%0A%09%7D%0A%09%2F%2F%20result%20is%20ambiguous%0A%09%2F%2F%20get%20IDs%20of%20all%20field%20groups%20for%20this%20post%0A%09%24field_groups_ids%20%3D%20array()%3B%0A%09%24field_groups%20%3D%20acf_get_field_groups(%20array(%0A%09%09’post_id’%20%3D%3E%20%24post_id%2C%0A%09)%20)%3B%0A%09foreach%20(%20%24field_groups%20as%20%24field_group%20)%0A%09%09%24field_groups_ids%5B%5D%20%3D%20%24field_group%5B’ID’%5D%3B%0A%09%0A%09%2F%2F%20Check%20if%20field%20is%20part%20of%20one%20of%20the%20field%20groups%0A%09%2F%2F%20Return%20the%20first%20one.%0A%09foreach%20(%20%24acf_fields%20as%20%24acf_field%20)%20%7B%0A%09%09if%20(%20in_array(%24acf_field-%3Epost_parent%2C%24field_groups_ids)%20)%0A%09%09%09return%20%24acf_fields%5B0%5D-%3Epost_name%3B%0A%09%7D%0A%09return%20false%3B%0A%7D%0A%0A%3F%3E » message= » » highlight= » » provider= »manual »/]