Query to sort a list by meta key first (if it exists), and show remaining posts without meta key ordered by title

I’m working on a custom taxonomy term page template where we want the items that are connected to the term sorted by a publication date (custom date field) – and if there are multiple items on the same day (formatted like YYYY-MM-DD) to then sort those by title, and finally sort by title if the custom field has not been filled out (older items).

So, I tried it hundred different ways with a WP_query and it does return most of the results as I want them – but in this case it’s only returning the items that have the meta_key of publication_date. All other items are being ignored and not displayed. I tried a meta_query using a relation of “or” and compared the publication_date as EXISTS and NOT EXISTS, but that returned 0 results for me.

Also, the site is running 3.5.2 still and they do not want to upgrade.

Here is my most recent query that gets me the posts that have the publication_date custom field displayed in the correct order:

$term = get_queried_object(); // find the term of the taxonomy page we are on
$wp_query = new WP_Query( array(
'post_type' => 'resource',
'tax_query' => array(
    array(
        'taxonomy' => 'resource_types',
        'field' => 'slug',
        'terms' => $term->name,
    )), 

'meta_key' => 'publication_date',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'paged' => $paged,
'posts_per_page' => '10',
));

I also tried using wpdb and running a SQL query, but I really am not sure how to accomplish what I want doing that. If someone could help me out that would be awesome!

Thanks in advance.

5

Thank you everyone for your help!

In the end the query below got me the results I desired – which was to show and sort the posts by a custom field of “publication_date” first – sorting by the date, and if there were multiple of the same date (say, 4 marked June 2013), it would sort those by title. Then, after it runs through all the posts that have the Publication Date filled in it will loop through again the remaining posts, alphabetically by title.

This gets me the results set in the same query, and keeps my pagination:

$term = get_queried_object();
the_post();
$wp_query = new WP_Query( array(
'post_type' => 'resource',
    'tax_query' => array(
        array(
            'taxonomy' => 'resource_types',
            'field' => 'slug',
            'terms' => $term->name,
        )),
 'meta_query' => array(
       'relation' => 'OR',
        array( //check to see if date has been filled out
                'key' => 'publication_date',
                'compare' => '=',
                'value' => date('Y-m-d')
            ),
          array( //if no date has been added show these posts too
                'key' => 'publication_date',
                'value' => date('Y-m-d'),
                'compare' => 'NOT EXISTS'
            )
        ),
'meta_key' => 'publication_date',
'orderby' => 'meta_value title',
'order' => 'ASC',
'paged' => $paged,
'posts_per_page' => '10',
));

Leave a Comment