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

$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){
        (post content here)

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

  • hmmm... i tried putting that before the loop and it makes the page return nothing. is there something i'm missing?
    – jamie
    Commented Oct 25, 2012 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
    Commented Oct 25, 2012 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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