• 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.

    https://conventionconnection.net

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    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
    	}
    }
    ?>

    Thread Starter Jeff F.

    (@jefferytodd)

    @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.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Drop down menu alphabetical list’ is closed to new replies.