I'm using a pre_get_posts filter found on this question thread here, to sort a custom post type in the dashboard:

function xyz_event_sort_order( $wp_query ) {
    if ( is_admin() ) {
        $post_type = $wp_query->query['post_type'];
        if ( $post_type == 'event' ) {
            $wp_query->set( 'meta_key', 'x_start_date' );
            $wp_query->set( 'orderby', 'x_start_date' );
            $wp_query->set( 'order', 'DESC' );
add_filter( 'pre_get_posts', 'xyz_event_sort_order' );

The filter sorts the events as expected, but events where the custom field has not been set (old events imported from another WP install) will, obviously, no longer show at all. I'm trying to do a meta_query that uses EXISTS and NOT EXISTS and still sorts by the meta_key specified above, but have had no luck.

Anybody know how I might also include events where x_start_date is missing and then add date as a second orderby option like: ( 'orderby', 'x_start_date date' );

  • what is the save date format ?
    – Shazzad
    Feb 25, 2014 at 1:00
  • @Shazzad, the format is 'Y-m-d'. The sort works correctly, it just needs to be modified to include cases where the key is not set. Maybe a meta_query with two different arrays using compare?
    – Sean M.
    Feb 25, 2014 at 2:17


Your Answer

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

Browse other questions tagged or ask your own question.