How to order dropdown by name?

Discussion in 'WordPress' started by Divvy, Oct 15, 2015.

  1. #1
    Hello again guys,

    Can someone tell me if is possible to order this dropdown by name instead of id?
    [​IMG]

    My code:
    
    <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>
    PHP:
    I need to order by post_title...

    Thank you!!
     
    Divvy, Oct 15, 2015 IP
  2. #2
    Yup, you just add more arguments.
    
    $args = array(
    'orderby' => 'something',
    'order' => 'DESC',
    );
    
    Code (markup):
    BTW, I suggest reading up on the WordPress codex. You'll find all the properties of functions and classes there.
    Also, you have a typo in the code you posted, mdoel instead of model.
     
    PoPSiCLe, Oct 15, 2015 IP
    sarahk likes this.
  3. Divvy

    Divvy Well-Known Member

    Messages:
    785
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #3
    Thank you once again my friend :D

    I changed:
    $args = array('post_type' => 'model',
    'post_status' => 'publish',
    'posts_per_page' => -1);
    PHP:
    With:
    $args = array('post_type' => 'model',
    'post_status' => 'publish',
    'orderby' => 'post_title',
    'order' => 'ASC',
    'posts_per_page' => -1);
    PHP:
    Is now working perfectly thanks to you :)
     
    Divvy, Oct 15, 2015 IP
  4. emily008

    emily008 Greenhorn

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #4
    use array sorting functions like uasort() etc.
     
    emily008, Oct 18, 2015 IP
  5. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #5
    @emily008 seriously? Are you dumb, or just wanting to up your post count? He's using WordPress. WordPress has built in functions for sorting this on the database layer, which is more efficient.
     
    PoPSiCLe, Oct 18, 2015 IP
    Spoiltdiva and sarahk like this.