[英]load my user_login from database into my acf select field code
我正在尝试将我的user_login从数据库上传到acf select字段,在var_dump中我的数组结果运行良好,但是我的acf选择字段中没有上传?
1.我在产品帖子中添加了自定义字段“ACF选择字段”。
2.我正在尝试从我的数据库加载我的选项值。
3.我使用数组从数据库中获取user_login值
function.php
add_filter('acf/load_field/name=chef', 'my_acf_load_chef_field');
function my_acf_load_chef_field( $field )
{
$user_fields = array( 'user_login');
$argu = new WP_User_Query( array( 'role' => 'chef' , 'fields' => $user_fields ));
$choices = $argu->get_results();
$field = array();
if( is_array($choices) ) {
$len = count($choices);
for($i = 0; $i < $len; $i++) {
array_push($field, ($choices[$i]->user_login));
}
}
// var_dump($field);
// exit;
return $field;
}
这是var_dump结果
array(5) {
[0]=> string(5) "Ahmed"
[1]=> string(5) "Khedr"
[2]=> string(4) "meme"
[3]=> string(5) "Menna"
[4]=> string(7) "mustafa" }
this is the array result that i want to load in acf field
您不会将您的选择分配给该字段。 试试这段代码:
if (is_array($choices)) {
// Clear the choices
$field[‘choices’] = [];
// Assign the data to the field
foreach ($choices as $choice) {
$field[‘choices’][$choice->user_login] = $choice->user_login;
}
}
我发现 这是新代码感谢justkidding96
add_filter('acf/load_field/name=chef', 'my_acf_load_chef_field');
function my_acf_load_chef_field( $field )
{
$user_fields = array( 'user_login');
$argu = new WP_User_Query( array( 'role' => 'chef' , 'fields' => $user_fields ));
$choices = $argu->get_results();
//$choices = get_field($field['choices'], $post->ID , false);
$field['choices'] = array();
if( is_array($choices) ) {
$len = count($choices);
for($i = 0; $i < $len; $i++) {
array_push($field['choices'], ($choices[$i]->user_login));
}
}
// var_dump($field['choices']);
// exit;
return $field;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.