Sort by two dates. Default entry date and custom field if present

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.

tech