If you add another tax_query
to the args, the last tax_query
will replace the previous tax_query
.
But anyway, if these are the criteria:
-
When filtering by
type
alone, show onlyresources
posts in thattype
. -
When filtering by
subject
alone, show onlyresources
posts in thatsubject
. -
When filtering by
type
andsubject
, show onlyresources
posts which are assigned to bothtype
andsubject
.
Then try this:
$args = array(
'post_type' => 'resources',
'post_status' => 'publish',
'posts_per_page' => 8,
);
// Filtering by both 'type' and 'subject'.
if ( $resource_type && $resource_subject ) {
$args['tax_query'] = array(
'relation' => 'AND',
array(
'taxonomy' => 'type',
'field' => 'slug',
'terms' => $resource_type,
),
array(
'taxonomy' => 'subject',
'field' => 'slug',
'terms' => $resource_subject,
),
);
}
// Filtering by 'type' only.
elseif ( $resource_type && ! $resource_subject ) {
$args['tax_query'] = array(
array(
'taxonomy' => 'type',
'field' => 'slug',
'terms' => $resource_type,
),
);
}
// Filtering by 'subject' only.
elseif ( $resource_subject ) {
$args['tax_query'] = array(
array(
'taxonomy' => 'subject',
'field' => 'slug',
'terms' => $resource_subject,
),
);
}