简体   繁体   English

将我的user_login从数据库加载到我的acf选择字段代码中

[英]load my user_login from database into my acf select field code

I am trying to upload my user_login from database into acf select field, in var_dump my array result is working good, but nothing uploaded in my acf select field? 我正在尝试将我的user_login从数据库上传到acf select字段,在var_dump中我的数组结果运行良好,但是我的acf选择字段中没有上传?

1. I added a custom field "ACF select field" in a product post. 1.我在产品帖子中添加了自定义字段“ACF选择字段”。

2. I'm trying to load my options values from my database. 2.我正在尝试从我的数据库加载我的选项值。

3. I used an array to get the user_login values from the database 3.我使用数组从数据库中获取user_login值

function.php 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;
}

this is the var_dump result 这是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

Your don't assign your choices to the field. 您不会将您的选择分配给该字段。 Try this code: 试试这段代码:

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

i found it this is the new code thanks justkidding96 我发现 这是新代码感谢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.

相关问题 新表字段上的Wordpress user_login字段 - Wordpress user_login field on new table field WordPress 数据库错误:[表 &#39;databasename.wp_users&#39; 不存在] SELECT * FROM wp_users WHERE user_login = &#39;Username&#39; - WordPress Database Error: [Table 'databasename.wp_users' doesn't exist] SELECT * FROM wp_users WHERE user_login = 'Username' 从acf选择字段加载所选内容 - load selected content from an acf select field 从数据库表动态填充Wordpress ACF中的选择字段 - Dynamically populate a select field in Wordpress ACF from a database table 如何检查 WordPress 中是否存在 user_login 并通过附加到现有 user_login 以编程方式创建新用户 - How to check if existing user_login in WordPress and create new user programatically by appending to an existing user_login ACF:从关系字段中加载转发器字段 - ACF: load repeater field from a relationship field 如何在我的网站产品添加页面中显示 ACF select 字段(下拉)? - how to display ACF select field(drop down) in my website product add page? 如何在我的产品发布页面中保存 ACF select 字段值(位置下拉)? - How to save ACF select field value(location drop down) in my product publish page? 更改电子邮件时更新用户名 (User_Login) (WooCommerce) - Update Username (User_Login) on Email Change (WooCommerce) 如何从用户配置文件中显示acf字段? - How to display an acf field from a user profile?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM