Support

Account

Home Forums Backend Issues (wp-admin) Dynamically Load Select Choices with Functions

Solved

Dynamically Load Select Choices with Functions

  • Hello,

    I’m loading ACF fields through a functions file. I’m trying to dynamically populate choices for a select field. I’ve done this successfully once using 'choices' => get_all_sites(), which shows in a var_dump (in single.php) to return array(5) { ["NULL"]=> string(19) "NULL (Waiting List)" [2]=> string(53) "RS of the SBB" [3]=> string(46) "RS of the IM" [4]=> string(47) "RS of the SMM" [5]=> string(49) "RS of the HHM" }

    I’m trying to do something similar with 'choices' => get_waiting_lists(), which shows in a var_dump (in single.php) to hold array(2) { [449]=> string(26) "Waiting List - SMM" [623]=> string(29) "Waiting List - SBB" }.

    I cannot for the life of me figure out why this second array won’t populate select choices. Does this make any sense to you or do you have any ideas? Or should I maybe be using a filter instead?

    Thank you!!

  • Hi @refreshingdesign

    Most likely, the issue is that the function does not work at the time when you are using it in the functions.php file.

    Can you check by debuging the value of get_waiting_lists() in the same file as your field group code? Does it work there, or only in single.php?

    If my suspicions are correct, you will need to use a filter to set the choices. If you take a look on the docs page, you will see an article which covers this exact query.

    Thanks
    E

  • I guess I need to use a filter. I modified the function to the following (so $field holds that array I was talking about in the initial post) and added the filter but still no luck… Any ideas? I really appreciate your help!

    function get_waiting_lists( $field ) {
    	
    	$field = array();
    
    		// Query Waiting Lists Products
    		$args = array(
    			'post_type' => 'memberpressproduct',
    			'orderby' => 'name',
    			'order' => 'ASC',
    			'posts_per_page' => -1,
    			'tax_query' => array(
    				array(
    					'taxonomy'  => 'topics',
    					'field'     => 'id',
    					'terms'     => '22',
    					'operator'  => 'IN'
    				),
    			)
    		);
    		$lists_array = get_posts( $args );
    		
    			foreach ( $lists_array as $list ) {
    				
    				$field[$list->ID] = $list->post_title;
    			
    			}
    	
    	wp_reset_postdata();
    
    	return $field;
    }
    
    add_filter('acf/load_field/name=select_waiting_list', 'get_waiting_lists');
  • Hi @refreshingdesign

    Your code is showing in incorrect use of the $field variable.

    This variable is not just for choices, it is for many pther things such as name / label etc.

    You need to be modifying the $field[‘choices’], not $field.

    Please let me know if this was not clear in the docs.

    Thanks
    E

  • Sorry about that – I believe we have the correct use of the variable now, but it doesn’t seem to be working correctly. Here’s everything I know:

    • We’ve registered the field 'name' => 'select_waiting_list', with 'choices' => array(), in functions.php.
    • AFTER registering all fields, still in functions.php, we used the modified filter as follows…
    function get_waiting_lists( $field ) {
    	
    	$field['choices'] = array();
    
    		// Query Waiting Lists Products
    		$args = array(
    			'post_type' => 'memberpressproduct',
    			'orderby' => 'name',
    			'order' => 'ASC',
    			'posts_per_page' => -1,
    			'tax_query' => array(
    				array(
    					'taxonomy'  => 'topics',
    					'field'     => 'id',
    					'terms'     => '22',
    					'operator'  => 'IN'
    				),
    			)
    		);
    		$lists_array = get_posts( $args );
    		
    			foreach ( $lists_array as $list ) {
    				
    				$choices[$list->ID] = $list->post_title;
    			
    			}
    	
    	$field['choices'] = $choices;
    		
    	wp_reset_postdata();
    	
    	return $field;
    }
    
    add_filter('acf/load_field/name=select_waiting_list', 'get_waiting_lists');
    
    • In single.php, this function dumps array(1) { ["choices"]=> array(2) { [449]=> string(26) "Waiting List - Millionaire" [623]=> string(29) "Waiting List - Small Business" } }
    • I’m not sure how to double check the value of this variable when we need it – which is in the admin while creating/editing a custom post type. When that field loads there, we get the select box with -Select- and then an empty choice beneath it. When inspecting the code, we see <select id="acf-field-connect_waiting_list" class="select" name="fields[field_5272b350a4105]"><option value="null">- Select -</option><option value="choices"></option></select>

    Thank you again, so much, for your help sorting this out. You guys have such great support.

    – Ross

  • Hi @refreshingdesign

    It is possible that there is a bug at the moment which would prevent the load_field filter from working witha field name. Can you try using the field key instead?

    In your filter, just change ‘name=select_waiting_list’ to ‘key=field_123’ – where this is the field key.

    Also, you can check if the filter is working by placeing this code before the return $field;

    
    print_r( $field );
    die;
    

    Thanks
    E

  • Banging my head on the wall now. name= works. I made a stupid mistake and had a duplicate field in my functions with a slightly different name and I was calling the wrong one. Thanks so much elliot! It’s great to understand the filters now.

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘Dynamically Load Select Choices with Functions’ is closed to new replies.