Good days. I found a better approach.
I have a custom function that looks at the multi-dimensional array and determines if they are empty.
Using that I found a way to dynamically add tax_query array into the main query for my wp_query.
I kept the “pool” in the main $args. I removed the month and tag. If the user selects a month and then a tag these new array items will be added to the tax_query.
It ended up looking like this:
$args = array(
'post_type' => 'daily-marriage-tips',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'cat_tips',
'field' => 'slug',
'terms' => $get_fym_dmt_pool,
),
)
);
// MONTHS ARRAY: check if the months array is empty
is_array_empty($current_post_id, $get_fym_dmt_month_arr);
if(is_array_empty($current_post_id, $get_fym_dmt_month_arr) == "not_empty"){
array_push(
$args['tax_query'],
array(
'taxonomy' => 'cat_months',
'field' => 'slug',
'terms' => array_for_query($current_post_id, $get_fym_dmt_month_arr),
)
);
}
// TAGS ARRAY: check if the tags array is empty
is_array_empty($current_post_id, $get_fym_dmt_tags_arr);
if(is_array_empty($current_post_id, $get_fym_dmt_tags_arr) == "not_empty"){
array_push(
$args['tax_query'],
array(
'taxonomy' => 'tag_tips',
'field' => 'slug',
'terms' => array_for_query($current_post_id, $get_fym_dmt_tags_arr),
)
);
}
My custom function is_array_empty accepts two arguments. The post id and the array you want to check. Here is what it looks like:
// Check if the array is empty
function is_array_empty($pID, $array) {
$item_arr = array();
foreach($array as $item_arr) {
if($item_arr[0] == NULL){
$status = "empty";
}else{
$status = "not_empty";
}
}
return $status;
}