As said here : Conditional arguments for WP_Query and tax_query depending on if $somevar has a value
I can define the args outside of the WP_Query instantiation:
<?php
$tax_query = array('relation' => 'AND');
if (isset($search_course_area))
{
$tax_query[] = array(
'taxonomy' => 'course-area',
'field' => 'id',
'terms' => $search_course_area
);
}
if (isset($search_course_level))
{
$tax_query[] = array(
'taxonomy' => 'study-levels',
'field' => 'id',
'terms' => $search_course_level
);
}
if (isset($search_course_mode))
{
$tax_query[] = array(
'taxonomy' => 'course-mode',
'field' => 'id',
'terms' => $search_course_mode
);
}
$query = new WP_Query(
array(
//Retreive ALL course posts
'post_type' => 'course',
'posts_per_page' => '-1',
//Filter taxonomies by id's passed from course finder widget
'tax_query' => $tax_query,
)
);
?>