I'm trying to order product post type with custom meta field for an expiration date.
I want to show all the expiring products (first being the expiring soon ones). After them, I want to show all the rest of the products that have an empty or missing expiration date after them in alphabetical order.
I have this in
$meta_query = array( 'relation' => 'OR', array( 'key' => 'expiring_date', 'value' => date('Ymd'), 'compare' => '>', 'type' => 'DATE' ), array( 'key' => 'expiring_date', 'compare' => 'NOT EXISTS' ), array( 'key' => 'expiring_date', 'value' => '' ), ); $query->set( 'meta_query', $meta_query ); $query->set('meta_key', 'expiring_date'); $query->set('orderby', array( 'meta_value_num' => 'ASC', 'title' => 'ASC' ));
The problem is that WordPress always shows the expiring products after the products with missing date. Changing the
DESC puts them at the before them as it should be but they are ordered from latest expiration to soonest.