Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WordPress Advanced Custom Fields get field key from field name
<?php
/**
* Get field key for field name.
* Will return first matched acf field key for a give field name.
*
* ACF somehow requires a field key, where a sane developer would prefer a human readable field name.
* http://www.advancedcustomfields.com/resources/update_field/#field_key-vs%20field_name
*
* This function will return the field_key of a certain field.
*
* @param $field_name String ACF Field name
* @param $post_id int The post id to check.
* @return
*/
function acf_get_field_key( $field_name, $post_id ) {
global $wpdb;
$acf_fields = $wpdb->get_results( $wpdb->prepare( "SELECT ID,post_parent,post_name FROM $wpdb->posts WHERE post_excerpt=%s AND post_type=%s" , $field_name , 'acf-field' ) );
// get all fields with that name.
switch ( count( $acf_fields ) ) {
case 0: // no such field
return false;
case 1: // just one result.
return $acf_fields[0]->post_name;
}
// result is ambiguous
// get IDs of all field groups for this post
$field_groups_ids = array();
$field_groups = acf_get_field_groups( array(
'post_id' => $post_id,
) );
foreach ( $field_groups as $field_group )
$field_groups_ids[] = $field_group['ID'];
// Check if field is part of one of the field groups
// Return the first one.
foreach ( $acf_fields as $acf_field ) {
if ( in_array($acf_field->post_parent,$field_groups_ids) )
return $acf_field->post_name;
}
return false;
}
@BenAttenborough

This comment has been minimized.

Copy link

@BenAttenborough BenAttenborough commented May 1, 2016

Awesome, this did the job to me. Seems strange ACF doesn't offer an easier way to discover the field keys given that it is necessary for lots of useful things. For example I want to let users edit and create custom post types on the front end, but I don't want to give them access to all the fields. In order to do that I need the field keys. It was most vexing trying to find them!

@fredclaymeyer

This comment has been minimized.

Copy link

@fredclaymeyer fredclaymeyer commented May 20, 2017

Thank you so much! I looked everywhere for how to do this. Context was how to get a select's options from within a repeater, for every post independent of a particular post ID.

@bodiequirk

This comment has been minimized.

Copy link

@bodiequirk bodiequirk commented Aug 23, 2017

This looks like just what I need, but embarrassingly, I don't know what to do with this file. Do I add the entire PHP file somewhere or just take the function into functions.php?

@lvl99

This comment has been minimized.

Copy link

@lvl99 lvl99 commented Sep 15, 2017

@bodiequirk copy+paste function into your theme's functions.php file, or you can save the file and require_once("/path/to/acf-get-field-key.php");.

@ambienthack

This comment has been minimized.

Copy link

@ambienthack ambienthack commented Oct 2, 2017

Thanks for sharing! It helped a lot. Looks like there is a bug on line 39. Should be
return $acf_field->post_name;

@Derrick974

This comment has been minimized.

Copy link

@Derrick974 Derrick974 commented Nov 26, 2018

dc2ed-5c539
9e224-1292e
ed9d8-c0707
5fbf5-8a14d
736f6-fce98
56ca0-ba9d7
8b149-cb6e6
c67f9-81158
58636-5a95c
259c9-de3bc
a607f-1a051
9b332-6da2d
68584-daf19
f20f2-024d4
2cb9e-a7bea
a505f-6971b

@Mulli

This comment has been minimized.

Copy link

@Mulli Mulli commented Dec 26, 2018

Smart! I was looking for such solution.
BUT, realizing that I shall generate zillion redundant accesses to the database
I shall simply build a map table, upon plugin activation.
Something like t(field-group, sub-field) = key-value
Using your code.
So I do it once and app user will not notice...
Thanks!!!

@ShinekhuuD

This comment has been minimized.

Copy link

@ShinekhuuD ShinekhuuD commented May 12, 2019

This code will blow up your database

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.