title: Filter WordPress custom post types by taxonomy term
body: <p>I have been struggling with this for a few days now and I seem to be no closer to a solution. I have been searching forums and tutorial sites, but have ended up more confused, as there seem to be a lot of ways and variations to achieve what I am looking for.</p>
  
  <p>What I want to do is create a Custom Post Type archive that can be filtered by taxonomy terms based on the url string.</p>
  
  <p><code>_domain/products/_</code></p>
  
  <p><code>_domain/products/taxonomy-term/_</code></p>
  
  <p><code>_domain/products/taxonomy-term/product-1_</code></p>
  
  <p>So the taxonomy term will only display custom posts of that type.</p>
  
  <p>I have got as far as this. Which seems to work for domain/products/taxonomy_term/product_1, but isn't picking up any archive templates.</p>
  
  <pre><code>    // define custom post types
      add_action( 'init', 'create_products' );
      function create_products() {
  
          register_post_type( 'products',
              array(
                  'labels' =&gt; array(
                      'name' =&gt; __( 'Products' ),
                      'singular_name' =&gt; __( 'Product' )
                  ),
              'public' =&gt; true,
              'has_archive' =&gt; true,
              'supports' =&gt; array( 'title', 'editor', 'excerpt', 'thumbnail', 'revisions' ),
              'rewrite' =&gt; array('slug' =&gt; 'products/%product_cat%', 'with_front' =&gt; true ),
              'hierarchical' =&gt; true,
              'query_var' =&gt; true,
              'show_in_nav_menus' =&gt; true,
              'menu_position' =&gt; 5
              )
          );      
      }
  
      add_action( 'init', 'create_my_taxonomies', 0 );
      function create_my_taxonomies() {
      register_taxonomy(
          'product_cat',
          'products',
          array(
              'labels' =&gt; array(
                  'name' =&gt; 'Product Categories',
                  'add_new_item' =&gt; 'Add New Product',
                  'new_item_name' =&gt; "New Product Category"
              ),
              'show_ui' =&gt; true,
              'show_tagcloud' =&gt; false,
              'hierarchical' =&gt; true,
              'rewrite' =&gt;  array( 'slug' =&gt; 'products', 'with_front' =&gt; true ),
              'query_var' =&gt; true,
          )
      );
  }
  
  function filter_post_type_link($link, $post)
  {
      if ($post-&gt;post_type != 'product_listing')
          return $link;
  
      if ($cats = get_the_terms($post-&gt;ID, 'product_cat'))
          $link = str_replace('%product_cat%', array_pop($cats)-&gt;slug, $link);
      return $link;
  }
  add_filter('post_type_link', 'filter_post_type_link', 10, 2);
  </code></pre>