is there a code i can add to sort a dropdown list in alphabetical order?

Viewed 190

I need to sort the location items in alphabetical order in this search form: http://v2trgmanagementcompanycom.temp312.kinsta.cloud/portfolio/

The locations should be listed alphabetically. Can someone help with updating the below code?

This is the current code:

<?php 
$array = OSF_Custom_Post_Type_Portfolio::getInstance()->get_all_meta_field_value();
?>

 <div class="search-project">
                <form action="<?php echo get_post_type_archive_link('osf_portfolio'); ?>" method="get">
                    <div class="d-flex justify-content-center project-group">
                  
                        <div class="project-inner">
                            <select name="osf_portfolio_type">
                                <option value=""><?php echo esc_html__('I'm Looking For', 'rehomes'); ?></option>
                                <?php
                                foreach ($array['osf_portfolio_type'] as $item) {
                                    ?>
                                    <option value="<?php echo esc_attr($item); ?>" <?php echo esc_attr($item == $_GET['osf_portfolio_type'] ? 'selected' : ''); ?>><?php echo esc_html($item); ?></option>
                                    <?php
                                }
                                ?>
                            </select>
                        </div>
                        <div class="project-inner">
                            <select name="osf_portfolio_location">
                                <option value=""><?php echo esc_html__('Location', 'rehomes'); ?></option>
                                <?php
                                foreach ($array['osf_portfolio_location'] as $item) {
                                    ?>
                                    <option value="<?php echo esc_attr($item); ?>" <?php echo esc_attr($item == $_GET['osf_portfolio_location'] ? 'selected' : ''); ?>><?php echo esc_html($item); ?></option>
                                    <?php
                                }
                                ?>
                            </select>
                        </div>
             
                        <div class="project-inner align-self-end">
                            <button type="submit" class="btn btn-primary btn-block" style="color:white;background-color: black;">
                                Search
                            </button>
                        </div>
                    </div>
                </form>
            </div>
1 Answers

You can use asort PHP function. check the below code.

<?php 
    $array = OSF_Custom_Post_Type_Portfolio::getInstance()->get_all_meta_field_value();
    asort($array['osf_portfolio_type']);
    asort($array['osf_portfolio_location']);
?>
<div class="search-project">
    <form action="<?php echo get_post_type_archive_link('osf_portfolio'); ?>" method="get">
        <div class="d-flex justify-content-center project-group">
            <div class="project-inner">
                <select name="osf_portfolio_type">
                    <option value=""><?php echo esc_html__('I\'m Looking For', 'rehomes'); ?></option>
                    <?php foreach ($array['osf_portfolio_type'] as $item) { ?>
                        <option value="<?php echo esc_attr($item); ?>" <?php echo esc_attr( $item == $_GET['osf_portfolio_type'] ? 'selected' : ''); ?> ><?php echo esc_html($item); ?></option>
                    <?php } ?>
                </select>
            </div>
            <div class="project-inner">
                <select name="osf_portfolio_location">
                    <option value=""><?php echo esc_html__('Location', 'rehomes'); ?></option>
                    <?php foreach ($array['osf_portfolio_location'] as $item) { ?>
                        <option value="<?php echo esc_attr($item); ?>" <?php echo esc_attr($item == $_GET['osf_portfolio_location'] ? 'selected' : ''); ?> ><?php echo esc_html($item); ?></option>
                    <?php } ?>
                </select>
            </div>
            <div class="project-inner align-self-end">
                <button type="submit" class="btn btn-primary btn-block" style="color:white;background-color: black;">
                    Search
                </button>
            </div>
        </div>
    </form>
</div>
Related