WordPress.org

Ready to get started?Download WordPress

Forums

Drop down menu alphabetical list (3 posts)

  1. Jeff F.
    Member
    Posted 1 year ago #

    Been trying to find an answer, I have a query string to fill in a drop down menu:

    query_posts(array(
    'posts_per_page' => -1,
    'post_type' => 'post',
    'orderby'=> 'name',
    'order' => 'ASC',
    'cat' => 30

    In order to make this work so the order of the drop down was alphabetical I had to make sure all the permalinks were "last name-firstname" but that's not how WordPress forms them automatically, it's first name first. Can the query be changed, I'm still new.

    http://conventionconnection.net

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    Try it with this in your functions.php:

    // A function to sort by last name (word).
    function lastNameSort( $a, $b ) {
    	$aLast = end( explode( ' ', $a->post_title ) );
    	$bLast = end( explode( ' ', $b->post_title ) );
    	return strcasecmp( $aLast, $bLast );
    }

    Now you can query like this:

    <?php
    $args = array(
    	'posts_per_page' => -1,
    	'post_type' => 'post',
    	'orderby'=> 'name',
    	'order' => 'ASC',
    	'cat' => 30
    );
    
    $posts = get_posts( $args );
    if ( $posts ) {
    
    	// sort the posts on the last word
    	uasort( $posts, 'lastNameSort' );
    
    	// loop through posts
    	foreach ( $posts as $post ) {
    		setup_postdata( $post );
    
    		// make your dropdown <option> here
    	}
    }
    ?>
  3. Jeff F.
    Member
    Posted 1 year ago #

    @keesiemeijer

    Thank you so much for posting this, I apologize for not replying sooner. They took down the site and changed the ftp so I can't try it yet. But I very much appreciate you taking the time and hope it works.

Topic Closed

This topic has been closed to new replies.

About this Topic