如何从WordPress数据库获取“高级自定义字段”字段键? [英] How to get Advanced Custom Fields field key from WordPress database?

查看:236
本文介绍了如何从WordPress数据库获取“高级自定义字段”字段键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有帖子类型的高级自定义字段。我有一些选择的自定义字段,我想显示每个字段中的所有标签选择。

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){


var_dump($ field)

var_dump($field)

显示为空:

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');

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) 
}

但是我有3个环境,我不想来对字段键进行硬编码。

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

有什么解决方案吗?
预先感谢。

Is there any solution? Thanks in advance.

推荐答案

此处是@BFDatabaseAdmin提供的答案的修改版本,与就像

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);
}

这篇关于如何从WordPress数据库获取“高级自定义字段”字段键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆