Support

Account

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

  • 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