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

I need to allow post ordering in certain categories. One of the sorting options is a custom field meta value. First problem is that not all posts have that meta assigned and i need to order these by date (default).

Another problem is that meta field should use ASC order and the rest DESC.

My code so far

In Template File:

$request = $_REQUEST;
global $query_string;
parse_str( $query_string, $qs_arr );

switch ( $request['sort'] ) {

    case 'title':
        $qs_arr['orderby'] = 'title';
        $qs_arr['order'] = 'ASC';
    case 'publisher':
        $qs_arr['meta_key'] = 'discography_publisher';
        $qs_arr['orderby'] = 'meta_value';

        add_filter( 'posts_orderby', 'sort_posts_orderby' );
        add_filter( 'get_meta_sql', 'sort_get_meta_sql' );
        add_filter( 'posts_where' , 'sort_posts_where' );

if ( isset( $request['sort'] ) ) { query_posts( $qs_arr ); }


function sort_get_meta_sql( $meta_sql ) {
    $meta_sql['join'] = " LEFT JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id AND wp_postmeta.meta_key = 'discography_publisher') ";
    return $meta_sql;

function sort_posts_where( $where ) {
    //here i remove the last statement "wp_postmeta.meta_key = 'discography_publisher' " so query return all posts in category 
    $explode = explode( 'AND', $where );
    array_pop( $explode );
    return implode( 'AND', $explode );

function sort_posts_orderby( $orderby ) {
    $orderby = 'COALESCE(wp_postmeta.meta_value, wp_posts.post_date) DESC';
    return $orderby;
share|improve this question

Assuming the rest works, why not order the normal way:

function sort_posts_orderby( $orderby ) {
    $orderby = 'wp_postmeta.meta_value ASC, wp_posts.post_date DESC';
    return $orderby;
share|improve this answer

I had something similar, where I ordered CPTs with an 'order' field, but not all of them had an assignement value, my order by :

      wp_postmeta.meta_value = '' ASC,
      wp_postmeta.meta_key ASC,
      wp_posts.post_date DESC

Results in ordering the posts with order field first, then orders the posts with no value by post date in descending order.

share|improve this answer
Hmm, didn't see the date, this question appeared at the top of my RSS feed... – Shane Dec 23 '11 at 17:33

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.