We help IT Professionals succeed at work.

Sorting custom posts by alphabetical order

rjunk
rjunk asked
on
487 Views
Last Modified: 2012-02-13
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>
Comment
Watch Question

Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
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(); ?>

Author

Commented:
Changing the call from get_posts to query_posts did the trick.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.