How to get Advanced Custom Fields field key from WordPress database? How to get Advanced Custom Fields field key from WordPress database? wordpress wordpress

How to get Advanced Custom Fields field key from WordPress database?


Here is a modified version of answer provided by @BFDatabaseAdmin matching the exact meta_value in "LIKE"

function get_acf_key($field_name) {  global $wpdb;  $length = strlen($field_name);  $sql = "    SELECT `meta_key`    FROM {$wpdb->postmeta}    WHERE `meta_key` LIKE 'field_%' AND `meta_value` LIKE '%\"name\";s:$length:\"$field_name\";%';    ";  return $wpdb->get_var($sql);}


Just trying to do this myself so I did some investigation. Seems each field and field group for ACF are stored in the wp_posts table in the database as custom post types. fields are 'acf-field' and groups are 'acf-field-group'.

I was able to use this function to get the field key to then use update_field($field_key, $value) on posts that didn't have the field already.

function get_acf_key($field_name){    global $wpdb;    return $wpdb->get_var("        SELECT post_name        FROM $wpdb->posts        WHERE post_type='acf-field' AND post_excerpt='$field_name';    ");}

Then I was able to use:

update_field(get_acf_key('my_field_name'), 'some value', $post_id);

To either update the field for posts that had it already or add the field and it's key reference to posts that did not already have the field.


I'm throwing another option into the mix. I think the existing answers are good, but unless you look at the parent group, you will never get a reliable field key because the field name can exist across multiple groups.

For example, lets say you have two custom groups - one for post type books, and one for custom post type movies. Both groups have added a field called title.

In the database, both are stored with post_except = 'title' and post_type = 'acf-field'. Two entries with the same post_except, so any query relying only on post_except will be wrong, wildcard or not.

Any query relying on post id is not great either, as a post might not always exist to pass in.

So you need to pass in a combination of field and group to get the field key from the name. This snippet works well for me:

if (! function_exists('acf_field_from_name')) {    function acf_field_from_name($field, $group)    {        global $wpdb;        return $wpdb->get_var($wpdb->prepare("            SELECT post.post_name as field_name            FROM $wpdb->posts AS post            LEFT JOIN $wpdb->posts AS parent                ON post.post_parent = parent.id            WHERE post.post_excerpt = %s                AND post.post_type = 'acf-field'                AND parent.post_excerpt = %s                AND parent.post_type = 'acf-field-group'        ", $field, $group));    }}

Will return the field key from name and group, or null if none exists.

Usage:

acf_field_from_name('title', 'movie-fields'); // returns field_3333333333333acf_field_from_name('title', 'book-fields'); // returns field_4444444444444acf_field_from_name('plumbus', 'movie'); // returns null