Get posts from 2 different categories

The first two methods aren’t working because the ‘cat=” and “category__and’ query variables are only for the standard post type (built in) Category taxonomy conditions.

The 3rd method is nested in multiple foreach loops which could be the cause of the duplication. I think it will work like you want if you just remove both those foreach loops and let the query stand on it’s own. Currently you are repeating the query for every term in both taxonomies.

Instead of using foreach, just grab the the current terms once by replacing this:

echo '<h1 style="margin-top:10px;">'.$custom_term->name.'</h1>';

with something like this:

$term1 = get_term_by('id', 6, 'ait-dir-item-special');
$term2 = get_term_by('id', 39, 'ait-dir-item-category');
echo '<h1 style="margin-top:10px;">'.$term1->name.' and '.$term2->name.'</h1>';