Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Is there a way for me to sort these posts alphabetically according to the custom field I have defined (last_name)?

<?php
$page_id = get_the_ID();

$args = array('sort_order'=>'DESC', 'post_type'=>'page', 'child_of'=>$page_id, 'post_status'=>'publish');
$posts_array = get_pages($args);


foreach ($posts_array as $post){
    setup_postdata($post);
    ?>
    <li>
        (post content here)
    </li>
    <?php
}
?>
share|improve this question

1 Answer 1

Not sure if this help. If you use normal wordpress loop to query post or pages then you can do this by using query_posts

You need to place a line of query_posts before the loop start and it might look something like this ( you might need to add some more parameter for getting only pages in your case )

query_posts($query_string. '&order=DESC&orderby=meta_value&meta_key=last_name')

Ref: http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

share|improve this answer
    
hmmm... i tried putting that before the loop and it makes the page return nothing. is there something i'm missing? –  jamie Oct 25 '12 at 17:26
    
ok; i had it in the wrong place. it needs to come after the main page loop becaues this is actually a landing page that pulls in a list of the children pages. this list of children pages is what has to be sorted alphabetically by the custom field value. it is doing nothing when i put it before the $page_id variable. –  jamie Oct 25 '12 at 18:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.