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>
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Changing the call from get_posts to query_posts did the trick.
ASKER
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(); ?>