I need to get the field key by field name in WordPress in the Advanced custom fields plugin (ACF).
The field is assigned to a post. I am in the loop of the post and I want to get the field key programmatically USING field name. The reason for this is because i'm creating a form where the field name 'options' will stay the same but will have different options in a select.
I am looping over a custom post type and expecting the field 'options' to be assigned to the post (each post will have unique options and so will have a unique field key so i can't just use the field key as this would be hard-coded to potentially another posts options)...
I'm in the loop of the post which should contain the custom field 'options' so I should be able to look for field key using post id and custom field name?
EDIT: I found this: https://gist.github.com/mcguffin/81509c36a4a28d9c682e
But it doesn't seem to work?
The right way is to use acf_maybe_get_field function, just like that:
acf_maybe_get_field( 'field_name', false, false );
The arguments are: field name, post id (defaults to current post) and the most important strict which defaults to true, but we set it to false here to get field object even when it not yet exists for the post.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With