phpwordpressadvanced-custom-fields

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?

1. I added a custom field "ACF select field" in a product post.

2. I'm trying to load my options values from my database.

3. I used an array to get the user_login values from the database

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

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

Solution

  • i found it this is the new code thanks 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;