WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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)
share|improve this question

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


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.