python wordpress自定义字段获取,如何从WordPress数据库获取“高级自定义字段”字段键?...

本文档描述了在使用Advanced Custom Fields插件时遇到的问题,即无法通过字段名称获取到选择字段的全部选项。作者尝试通过`get_field_object`函数获取字段对象,但在某些情况下返回的数据中缺少`choices`属性。为了解决这个问题,提供了一个修改后的SQL查询函数`get_acf_key`,该函数能根据字段名称匹配并返回完整的字段对象,从而获取到所有选择项。此解决方案对于不希望硬编码字段键且在多个环境中运行的情况非常有用。

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.

解决方案

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

}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值