Support

Account

Home Forums General Issues ACF Dropdown populated by query Reply To: ACF Dropdown populated by query

  • Yes, here’s an example:

    The code below adds the roster for a team based on certain criteria.

    function add_away_rosters_select($field){
    		
    		$sports_cat = array('boys-basketball','girls-basketball','boys-hockey','girls-hockey','baseball','softball','boys-lacrosse','girls-lacrosse','boys-tennis','girls-tennis');
    
    		$cat = get_the_category();
    		$sport = $cat[0]->slug;
    
    		$field['choices'] = array();
    		
    		if( in_array($sport,$sports_cat) ){
    
    		$away = get_field('away_team_2')->ID;
    
    		$args_1['post_type'] = 'roster';
    		$args_1['post_status'] = 'publish';
    		$args_1['order_by'] = 'title';
    		$args_1['order'] = 'ASC';
    		$args_1['meta_key'] = 'school_team';
    		$args_1['meta_value'] = $away;
    		$args_1['category_name'] = $sport;
    		$args_1['posts_per_page'] = -1;
    
    		$away_players = get_posts($args_1);
    
    		$away_roster = array();
    	
    		for($i = 0; $i < count($away_players); $i++){
    		
    			$away_player = get_field('last_name',$away_players[$i]->ID).", ".get_field('first_name',$away_players[$i]->ID);	
    			$away_player_ID = $away_players[$i]->ID;
    			$away_roster[$away_player_ID] = $away_player;	
    	
    		}
    
    		asort($away_roster);
    
                
                $field['choices'] = $away_roster;
                
            
    
    }
    
    		return $field;
    
    }
    
    // changes the choices to players from the away rosters
    
    add_filter('acf/load_field/key=field_58dadbb26c60a', 'add_away_rosters_select');