'Custom Post Type filtering by own taxonomies

I tried a lot, but with no success. This LOOP works fine. It shows all Custom Post Types (apps-und-tools):

<?php
$loop = new WP_Query(
     array(
          'post_type' => 'apps-und-tools',
          'posts_per_page' => -1,
     )
);
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div>
    <h2><?php the_title(); ?></h2>
</div>
<?php endwhile;
wp_reset_postdata();

Now I want to filter by a custom taxonomy. First I get the custom taxonomy with get_queried_object() and put the tax_query to the array in the LOOP. At the end are some control outputs.

<?php
// getting custom taxonomy of actual post
$this_term = get_queried_object();
$term_id =  $this_term->term_id;
$term_name =  $this_term->name;
$term_taxonomy = $this_term->taxonomy;
$term_slug = $this_term->slug;

// Wordpress Loop
$loop = new WP_Query(
     array(
          'post_type' => 'apps-und-tools',
          'posts_per_page' => -1,
          'tax_query' => array(
               array (
                    'taxonomy' => $term_taxonomy,
                    'name' => $term_name
          )
          )
     )
);
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div>
    <h2><?php the_title(); ?></h2>
</div>
<?php endwhile;
wp_reset_postdata();

//Control Output
echo 'term_id: ' . $this_term->term_id;
echo '<br>';
echo 'name: ' . $this_term->name;
echo '<br>';
echo 'taxonomy: ' . $this_term->taxonomy;
echo '<br>';
echo 'slug: ' . $this_term->slug;
?>

The result is, that the LOOP does not show any results. No CPTs are shown, but the control output is as expected. I'd just like to filter my CPTs by custom taxonomy.

Any help is greatly appreciated, many thanks in advance.



Solution 1:[1]

Please check this loop to retrieve CPTs.

$args = array(
 'post_type' => arra('apps-und-tools'),
'tax_query' => array(
    array(
        'taxonomy' => $term_taxonomy,
        'field'    => 'slug',
        'terms'    => $term_slug,
    ),
 ),
);
$query = new WP_Query( $args );

Solution 2:[2]

$term_taxonomy = 'taxonomy_name';
$args = array(
  'post_type' => arra('apps-und-tools'),
  'post_status' => "publish",
  'tax_query' => array(
    array(
      'taxonomy' => $term_taxonomy,
      'field'    => 'slug',
      'terms'    => $search_key,
      ),
    ),
  );
$query = new WP_Query( $args );

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hritik Pandey
Solution 2 Dotsquares