If I want to sort on 1) menu_order and if the pages weight the same I want to 2) sort them alphabetically, how? Is this the default behavior if you set menu_order?
Is it?:
$args = array(
'sort_order' => 'ASC',
'sort_column' => 'menu_order, post_title'
);
$children = get_pages($args);
Or?:
$args = array(
'sort_order' => 'ASC',
'sort_column' => 'menu_order post_title'
);
Or?:
$args = array(
'sort_order' => 'ASC',
'sort_column' => 'menu_order | post_title'
);
Or?:
$args = array(
'sort_order' => 'ASC',
'sort_column' => array('menu_order', 'post_title')
);
etc…
From wp_query it seems like it would be a simple space? Multiple orderby values in WP_Query
Should really be in the spec.!
get_pages
explodessort_column
on the comma (,
) character. Just check the source:So the correct format should be the first one you posted:
If you are not seeing the ordering you expect it is probably because of how the
ASC
is applied. TheASC
sort order is going to be applied to the last item in the list, not to all of them.That
ASC
is tacked onto the SQL after the completeORDER BY
statement resulting in something like this:ORDER BY menu_order, post_title ASC
. I suspect that you wantORDER BY menu_order ASC, post_title
orORDER BY menu_order ASC, post_title ASC
instead.To get either of those results, you will need to use
WP_Query
because the SQL used byget_pages
is pretty rigid and not easily filtered as it uses$wpdb->get_results
directly.You will still need a filter, though, as
WP_Query
handlesorder
the same way thatget_pages
does.This also works perfectly in wp_list_pages: