我在post类型中使用高级自定义字段。我有一些选择自定义字段,我希望显示每个字段中的所有标签选择。
我试过这种方法。
$field = get_field_object('hair_color');
$hair = $field["choices"];
foreach($hair as $value){
做一个
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)
}
把它填满的唯一方法是:
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个环境,我不想硬编码字段键。
有什么解决方案吗?提前谢谢。
发布于 2015-09-06 12:04:25
我自己也想这么做,所以我做了些调查。似乎ACF的每个字段和字段组都作为自定义post类型存储在数据库的wp_posts表中。字段是'acf-field‘,组是'acf-field-group’。
我可以使用这个函数来获取字段键,然后在还没有字段的帖子上使用update_field($field_key,$value)。
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';
");
}
然后我可以使用:
update_field(get_acf_key('my_field_name'), 'some value', $post_id);
以更新已有字段的帖子的字段,或者添加该字段及其对尚未具有该字段的帖子的关键引用。
发布于 2016-01-21 20:10:34
以下是@BFDatabaseAdmin提供的答案的修改版本,与"LIKE“中的meta_value完全匹配。
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);
}
发布于 2016-12-12 21:31:42
我把另一个选项加入到这个组合中。我认为现有的答案很好,但除非您查看父组,否则您永远不会获得可靠的字段键,因为字段名称可能存在于多个组中。
例如,假设您有两个定制组-一个用于post类型的图书,另一个用于自定义post类型的电影。两个组都添加了一个名为title的字段。
在数据库中,两者都存储在post_except = 'title'
和post_type = 'acf-field'
中。两个条目具有相同的post_except
,因此任何仅依赖post_except
的查询都将是错误的,无论是否使用通配符。
任何依赖post id的查询也不是很好,因为可能并不总是存在需要传递的post。
因此,您需要传入字段和组的组合,才能从名称中获取字段键。下面这段代码对我来说很好用:
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));
}
}
将从name和group返回字段键,如果不存在,则返回null。
用法:
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
https://stackoverflow.com/questions/21184015
复制相似问题