2

I have a custom post type where there is an option to sort by date. There is however a custom date field that allows changing the display date. This custom date field is not always filled.

Right now I'm trying to sort by date, and if the custom date field is present use that field when sorting instead.

I have the following that doesn't really work since I believe orderby sorts by the custom field and then tacks on sorted entry dates.

$args = array( 
    'post_type' => array('post'),
    'order'     => 'DESC',
    'orderby'   => array('upcoming_class_date' => 'DESC', 'date' => 'DESC'),
    'category_name'   => 'upcoming class',
    'meta_key'      => 'company',
    'meta_value'    => $company,            
    'showposts' => 40
);

$loop = new WP_Query( $args );

I was thinking of grabbing the results and resorting them after query, but that seems very inefficient.

1

Few issues I see here with your code:

  1. You have category_name set to upcoming class ... category slugs do not have spaces in them, so this is incorrect. If you meant to have all upcoming and class categories, then use upcoming,class https://developer.wordpress.org/reference/classes/wp_query/#category-parameters

  2. You have a meta query on company but it sounds like you're also trying to sort by a custom field upcoming_class_date, and as such, this field also needs to be included in your query. You will need to change this to an advanced meta query to handle this. https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters

  3. You said a custom post type -- but your code has post as the post type

This is some quick example code I came up with on how you should setup your query arguments:

$args = array(
    'post_type'     => array( 'post' ),
    'order'         => 'DESC',
    'orderby'       => array( 'upcoming_class_date' => 'DESC', 'date' => 'DESC' ),
    'category_name' => 'upcoming class',
    'showposts'     => 40,
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key'     => 'company',
            'value'   => $company,
            'compare' => '=',
        ),
        array(
            'relation' => 'OR',
            array(
                'key'     => 'upcoming_class_date',
                'compare' => 'EXISTS',
            ),
            array(
                'key'     => 'upcoming_class_date',
                'compare' => 'NOT EXISTS',
            )
        )

    ),
);

You will notice there is an EXISTS and NOT EXISTS .. this is because you said that sometimes a post will not have a value for that field -- to make sure the value is included in the query, we have to add both of those and set the relation to OR (that way it returns posts with that field and ones without).

The other potential issue I see is the format the date is saved in -- which could cause sorting problems, but you did not provide details on the format.

I recommend reading this answer on a similar question to better understand how WP_Query works with meta queries: https://wordpress.stackexchange.com/a/285012/51201

You can also do an orderby using the meta query array key (but they have to be changed to associative array) -- there are examples in the link above for that.

  • Thanks for the fantastic response. On a strange note, I know that category_name should be using the slugs, but when I run the query correctly I get no results. With spaces, works fine. Weird, right? – Chris Apr 22 at 23:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.