How to order dropdown by name?

Status
Not open for further replies.

Divvy

Active Member
806
2009
18
0
Hello guys,

Can someone tell me if is possible to order this dropdown by name instead of id?
UGwufpQ.png


My code:
PHP:
<select name="mr_freeGalleryModel" id="mr_freeGalleryModel">
               <option value="">Select</option>
               <?php
               //$mr_ModelId =get_post_meta($mrpostParent,"mr_freeGalleryModel",true);
                
               $args = array('post_type' => 'model',
                    'post_status' => 'publish',
                    'posts_per_page' => -1);
              
                $my_query = null;
                $my_query = new WP_Query($args);
                if( $my_query->have_posts() )
                {
                 while ($my_query->have_posts()) : $my_query->the_post();              
                  $mdoel_id = get_the_ID();
                
                    echo '<option value="'.$mdoel_id.'">'.get_the_title().'</option>';
                  
                  endwhile;              
                }
                wp_reset_query();
              ?>
               </select>

I need to order by post_title...

Thank you!!
 
2 comments
PHP:
<select name="mr_freeGalleryModel" id="mr_freeGalleryModel"> 
               <option value="">Select</option> 
               <?php 
               //$mr_ModelId =get_post_meta($mrpostParent,"mr_freeGalleryModel",true); 
                 
               $args = array('post_type' => 'model', 
                    'post_status' => 'publish', 
                    'posts_per_page' => -1); 
               
                $my_query = null; 
                $my_query = new WP_Query($args); 
                $temp=array();

                if( $my_query->have_posts() ) 
                { 
                 while ($my_query->have_posts()) : $my_query->the_post();               
                  $mdoel_id = get_the_ID(); 
                 
                    //echo '<option value="'.$mdoel_id.'">'.get_the_title().'</option>'; 
                   $temp['get_the_title()'] ='<option value="'.$mdoel_id.'">'.get_the_title().'</option>';
                  endwhile;               
                } 
                wp_reset_query(); 
                ksort($temp);
                foreach($temp as $temp_obj)
                 echo $temp_obj;
              ?> 
               </select>


should help i guess.

EDIT: Sorry I missed that, @olli460 has better solution ;)
 
Last edited:
Status
Not open for further replies.
Back
Top