Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

using two orderby in a query WordPress


I have a custom post type created, where there will be
title field and custom date field is used.

The scenario is, when I display the result in wp query.

I must ensure the output is in alphabetical order and also the the dates should be in ascending order.


alex 30-may-2014
peter 28-may-2014
Paul 29-may-2014

You can see that it alphabetically arranged and also peter comes in first place as the date is prior to paul.

I will give more brief if needed.

The solution I needed cant be achieved exactly via wordpress functions.
So I have made some custom functions to achieve the end result.

thanks for every ones contribution.

This is my first question in Wpquestion, so I am gonna spread the money based on the effort.

Answers (4)


Sébastien | French WordpressDesigner answers:

maybe could you retrieve the first letter of the name in an hidden custom fiel "firstletter" and sort your posts by "firstletter" and date.

1-retrieve the first character with [[LINK href=""]]mb_substr[[/LINK]]
2-save this value with add_post_meta

You may also be interested in this solution :

And according to the Codex, you simply need to separate them by a space like that :
'orderby' => 'firstletter date'

zebra webdesigns comments:

Hello sebastien,

Orderby takes only one parameter in the query.
thats why I am looking for any solution.

Correct me if I am wrong and also the precise answer / working solution is appreciated.

Sébastien | French WordpressDesigner comments:

according to the Codex, you simply need to separate them by a space like that :
'orderby' => 'firstletter date'

have you try ?

zebra webdesigns comments:

Can you give the codex link.

Sébastien | French WordpressDesigner comments:

Sébastien | French WordpressDesigner comments:

in the codex :
<blockquote>Display posts sorted by menu_order with a fallback to post title, in a descending order:

$query = new WP_Query( array ( 'orderby' => 'menu_order title', 'order' => 'DESC' ) );

Sébastien | French WordpressDesigner comments:

as i write in my first response, You may also be interested in this solution :


Martin Pham answers:

Please show full query, i can help you ./.

zebra webdesigns comments:

Hello Martin,

this is normal query only for custom post type.

except the orderby should work on two parameters.

so the shorting should implement both.

Martin Pham comments:

Try filter:

function multiple_posts_orderby($orderby) {
global $wpdb;
$orderby = "$wpdb->posts.post_title DESC, $wpdb->postmeta.meta_value ASC";
return $orderby;

add_filter('posts_orderby', 'multiple_posts_orderby');


Dbranes answers:

You can always use the <em>posts_orderby</em> filter to manipulate the order to your needs.

For example:

add_filter( 'posts_orderby', 'wpq_orderby' );
$new_query = new WP_Query( $args );

function wpq_orderby( $order ){
remove_filter( current_filter(), __FUNCTION__ );

// mix your own custom order here:
// ...

return $order;

Dbranes comments:

You can order by two filelds (seperated by a space) in the order parameter as Sébastien suggests and it works, but be aware that they will have the same order directions, i.e. either both ASC or both DESC.

zebra webdesigns comments:

Will check it Dbranes, but is it working with default title and custom field date ?

Dbranes comments:

It should only work for the following fields:

'author', 'post_author', 'date', 'post_date', 'title', 'post_title', 'name', 'post_name', 'modified',
'post_modified', 'modified_gmt', 'post_modified_gmt', 'menu_order', 'parent', 'post_parent',
'ID', 'rand', 'comment_count'

but not custom fields, so using the <em>posts_orderby </em>filter will help you to cross that limitation.


timDesain Nanang answers:

you can use WP_Query or custom query.

<strong>i assume that </strong>
post_type is person
meta_field is register

try this:

<strong>WP_Query </strong>

$post_type = 'person';
$meta_key = 'register';
$args = array(
'post_type' => $post_type,
'post_status' => array('publish'),
'posts_per_page' => 5,
'meta_key' => $meta_key,
'orderby' => 'meta_value title',
'order' => 'DESC',
'meta_query' => array(
'key' => $meta_key,
'value' => '',
'compare' => '!=',
$wpq = new WP_Query($args);
if ( $wpq->have_posts() ) {
while ( $wpq->have_posts() ) {
$meta_values = get_post_meta( $post->ID, $meta_key, true );
<td><?php the_ID();?></td>
<td><?php echo $meta_values;?></td>
<td><?php the_title();?></td>


<strong>Custom Query</strong>

$post_type = 'person';
$meta_key = 'register';
$qry_str = "
FROM $wpdb->posts p, $wpdb->postmeta m
WHERE p.ID = m.post_id
AND p.post_type = '".$post_type."'
AND p.post_status = 'publish'
AND m.meta_key = '".$meta_key."'
AND m.meta_value <> ''
ORDER BY m.meta_value DESC, p.post_title ASC
$qry_res = $wpdb->get_results($qry_str);
if ($qry_res){
foreach ($qry_res as $post){
$meta_values = get_post_meta( $post->ID, $meta_key, true );
<td><?php the_ID();?></td>
<td><?php echo $meta_values;?></td>
<td><?php the_title();?></td>