Skip to content

Instantly share code, notes, and snippets.

@mcguffin
Last active June 13, 2023 13:32
Show Gist options
  • Star 36 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save mcguffin/81509c36a4a28d9c682e to your computer and use it in GitHub Desktop.
Save mcguffin/81509c36a4a28d9c682e to your computer and use it in GitHub Desktop.
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;
}
@Mulli
Copy link

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
Copy link

This code will blow up your database

@Hollyw00d
Copy link

Hollyw00d commented Jan 18, 2023

Thank you!

If you need to use this code outside your theme and don't have access to ACF functions (like in another WordPress plugin) you can include a line similar to below to reference the ACF plugin (like ACF Pro plugin):

include_once WP_PLUGIN_DIR .'/advanced-custom-fields-pro/acf.php';

@Hollyw00d
Copy link

This code will blow up your database

@ShinekhuuD can you please tell us an alternative if you don't think this is a good idea?

@Mulli
Copy link

Mulli commented Jan 18, 2023

@Hollyw00d Allow me. There are few ways:

  1. Use get field object - if you have the exact selector, its simple
  2. Parse the acf-json/*.json and extract keys from there. It can done on the fly Or you may generate your own index
    Hope that helps

@Hollyw00d
Copy link

@Mulli I used your suggestion below to add the ACF field ID (like field_63c84b9cb5f66):

Parse the acf-json/*.json and extract keys from there. It can done on the fly Or you may generate your own index

I can't display the code here as I used it for my job but this another option to get the ACF field ID without making database calls.

@marian-kadanka
Copy link

Or one can simply use acf_get_reference()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment