How to order query results based on if a custom field has been populated, then the order by the date of the post?

I am trying to custom sort a query for product posts (custom content type) based on if a specific ACF custom field for a product ID number has been populated. My client wants to feature products with this first as it adds extra details for inventory.

My current script works to a degree, but doesn't seem to factor date into the order/sort so I am assuming it is sorting based on the actual product ID number, versus if it just exists or not, then date for the sort order beyond that.

        $args = array(
            'meta_query' = array(
                'relation' = 'OR',
                array(
                    'key' = 'product_identifier',
                    'compare' = 'NOT EXISTS',
                ),
                array(
                    'key' = 'product_identifier',
                    'compare' = 'EXISTS',
                ),
            ),
            'orderby' = 'meta_value date',
            'order' = 'DESC DESC',
            'post_type' = 'products',
            'offset' = $offset,
            'posts_per_page' = $ipp,
        );

UPDATE: So far my answer has been to add a second custom field that is just a checkbox to say to feature this product first. Being that sets the same value to each, I can then order by it and the fall back to date then works it seems.

Topic meta-query wp-query custom-post-types sort Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.