How to get Advanced Custom Fields field key from W

2019-03-18 01:46发布

I’m using Advanced Custom Fields with post-type. I have some select custom fields, and I want to show all the label choices from each field.

I’ve tried this way.

$field = get_field_object('hair_color');
$hair = $field["choices"];
    foreach($hair as $value){

Doing a

var_dump($field)

it appears empty:

array(18) { 
   ["key"] => string(16) "field_hair_color" 
   ["label"] => string(0) "" 
   ["name"] => string(10) "hair_color" 
   ["_name"] => string(10) "hair_color" 
   ["type"]=> string(4) "text" 
   ["order_no"]=> int(1) 
   ["instructions"]=> string(0) "" 
   ["required"]=> int(0) 
   ["id"] => string(20) "acf-field-hair_color" 
   ["class"] => string(4) "text" 
   ["conditional_logic"] => array(3) { 
        ["status"] => int(0) 
        ["allorany"]=> string(3) "all" 
        ["rules"]=> int(0) 
   } 
   ["default_value"] => string(0) "" 
   ["formatting"] => string(4) "html" 
   ["maxlength"] => string(0) "" 
   ["placeholder"] => string(0) "" 
   ["prepend"] => string(0) "" 
   ["append"] => string(0) "" 
   ["value"] => bool(false) 
}

The only way to get it full is that:

get_field_object('field_51ac9d333d704');

array(17) { 
   ["key"] => string(19) "field_51ac9d333d704" 
   ["label"] => string(13) "Color de pelo" 
   ["name"] => string(10) "hair_color" 
   ["_name"] => string(10) "hair_color" 
   ["type"] => string(6) "select" 
   ["order_no"] => int(9) 
   ["instructions"] => string(27) "Selecciona el color de pelo" 
   ["required"] => int(0) 
   ["id"] => string(20) "acf-field-hair_color" 
   ["class"] => string(6) "select" 
   ["conditional_logic"] => array(3) { 
       ["status"] => int(0) 
       ["rules"] => array(1) { 
           [0] => array(3) { 
               ["field"] => string(19) "field_5195ef9879361" 
               ["operator"] => string(2) "==" 
               ["value"] => string(5) "small" 
           } 
       } 
       ["allorany"] => string(3) "all" 
   } 
   ["choices"] => array(5) { 
        ["bald"] => string(5) "Calvo" 
        ["brown"] => string(8) "Castaño" 
        ["brunette"] => string(6) "Moreno" 
        ["red"] => string(9) "Pelirrojo" 
        ["blonde"] => string(5) "Rubio" 
    } 
    ["default_value"] => string(0) "" 
    ["allow_null"] => int(1) 
    ["multiple"] => int(0) 
    ["field_group"] => int(90679) 
    ["value"]=> bool(false) 
}

But I have 3 environment, and I don’t want to hardcode the field key.

Is there any solution? Thanks in advance.

7条回答
欢心
2楼-- · 2019-03-18 02:48

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_3333333333333

acf_field_from_name('title', 'book-fields'); // returns field_4444444444444

acf_field_from_name('plumbus', 'movie'); // returns null
查看更多
登录 后发表回答