Link to home
Start Free TrialLog in
Avatar of rjunk
rjunk

asked on

Sorting custom posts by alphabetical order

Hello,
I have built a WP site for a small non-profit.  

On the "Who We Are" page, there are different lists of names: staff members, advisory boards, etc.  

I have set this page up as a series of custom posts.

The client would like to order these lists alphabetically by last name.

I did some research and found a way to customize the loop that should alphabatize by last name.  

However, I can only get the loop to output one name: it is not displaying all the names in the list.  

My questions are: what am I missing?  And, is there a more elegant way to do this?

The page in question is here: http://babybuggy.org/what-we-do/who-we-are/
The test list is towards the bottom of the page; it is called "Friends Alpha Test".

The code I used is below.

Thank you very much!

<h4>Friends Alpha Test</h4>
               <?php
            $lastname = array();
$posts = get_posts( array('post_type' => 'people', 'whoweare' => 'friends', 'numberposts'  => 100) );

foreach( $posts as $key => $post ) {

      $lastname[$key] = array_pop( explode( ' ', $post->post_title ) );
}
array_multisort( $lastname, SORT_ASC, $posts );
           
                   if ( have_posts() ) : while ( have_posts() ) : the_post();
                  ?>
             <li><?php the_title(); ?></li>
             <li><?php the_content(); ?></li>
            <?php endwhile; endif; wp_reset_query(); ?>
       </ul>
ASKER CERTIFIED SOLUTION
Avatar of EMB01
EMB01
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rjunk
rjunk

ASKER

Thanks, EMB01. I did as you asked, though I am a PHP nitwit, so I may have bungled this a bit.  Still, it returns a full list of names, albeit with much other db info:
http://babybuggy.org/what-we-do/who-we-are/

This is the current call:
  <?php
            $lastname = array();
$posts = get_posts( array('post_type' => 'people', 'whoweare' => 'friends', 'numberposts'  => 100) );

var_dump($posts); {

      $lastname[$key] = array_pop( explode( ' ', $post->post_title ) );
}
array_multisort( $lastname, SORT_ASC, $posts );
           
                   if ( have_posts() ) : while ( have_posts() ) : the_post();
                  ?>
             <li><?php the_title(); ?></li>
             <li><?php the_content(); ?></li>

 
            <?php endwhile; endif; wp_reset_query(); ?>
Avatar of rjunk

ASKER

Changing the call from get_posts to query_posts did the trick.