Orderby event date (if it exists) or post date (if it doesn’t) – how to reorder posts based on meta value OR date

Based on

  1. Can I exclude a post by meta key using pre_get_posts function? // setting meta in pre_get_posts
  2. Query all posts where a meta key does not exist // exists & not exists formatting
  3. multiple orderby in pre_get_posts action // how to set multiple ordeby

you could perhaps try something like this in your pre_get_posts filter,

//Get original meta query
$meta_query = $query->get('meta_query');

//Add our meta query to the original meta queries
// Posts that have and don't have the meta value
$meta_query[] = array(
  'relation' => 'OR',
  array(
   'key'     => 'event_start',
   'compare' => 'NOT EXISTS',
   'value'   => ''
  ),
  array(
   'key'     => 'event_start',
   'compare' => 'EXISTS'
  )
);
$query->set('meta_query',$meta_query);

$query->set('orderby', array('meta_value' => 'DESC', 'date' => 'DESC'));

My apologies, I couldn’t test this code example as I didn’t have similar data in any of my WP sandboxes, and it’s a bit late as I’m writing this so I might have missed something, so take it with a grain of salt.