Sorting Posts by a Custom Field
-
I have a post template which displays the custom fields of all posts within a category as tabular data — think of a sports team roster page
How can I control the sort order of the posts coming in?
I want to sort by the player’s number
<!-- GET ANY POSTS ASSIGNED WITH THE CUSTOM FIELD PLAYER-SPORT THAT EQUALS VOLLEYBALL --> <?php query_posts('meta_key=player-sport&meta_value=wbb'); ?> <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <?php $playerfirstname = get_post_meta($post->ID, 'player-first-name', true); ?> <?php $playerlastname = get_post_meta($post->ID, 'player-last-name', true); ?> <?php $playeryear = get_post_meta($post->ID, 'player-year', true); ?> <?php $playerposition = get_post_meta($post->ID, 'player-position', true); ?> <?php $playernumber = get_post_meta($post->ID, 'player-jersey-number', true); ?> <?php $playerhometown = get_post_meta($post->ID, 'player-hometown', true); ?> <?php $playerheight = get_post_meta($post->ID, 'player-height', true); ?> <?php $playerimage = get_post_meta($post->ID, 'player-image', true); ?> <?php $playerhighschool = get_post_meta($post->ID, 'player-highschool', true); ?> <tr> <td><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><? if($playerimage) : ?><img src="<? echo $playerimage ?>" alt="Yavapai College <? echo $playeryear ?> <? echo $playerfirstname ?> <? echo $playerlastname ?>" title="Yavapai College <? echo $playeryear ?> <? echo $playerfirstname ?> <? echo $playerlastname ?>" width="100" style="border:2px solid #333;"></a><?php endif; ?></td> <td><a style="color:blue; text-decoration:none; border-bottom:1px dotted blue; font-weight:bold;" href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><? if($playerlastname) : ?> <? echo $playerlastname ?><?php endif; ?>, <? if($playerfirstname) : ?><? echo $playerfirstname ?><?php endif; ?></a></td> <td><? if($playernumber) : ?><? echo $playernumber ?><?php endif; ?></td> <td><? if($playeryear) : ?><? echo $playeryear ?><?php endif; ?></td> <td><? if($playerposition) : ?><? echo $playerposition ?><?php endif; ?></td> <td><? if($playerheight) : ?><? echo $playerheight ?><?php endif; ?></td> <td><? if($playerhometown) : ?><? echo $playerhometown ?><?php endif; ?></td> <td><? if($playerhighschool) : ?><? echo $playerhighschool ?><?php endif; ?></td> </tr>
- The topic ‘Sorting Posts by a Custom Field’ is closed to new replies.