title: Dynamically populate taxonomy term dropdown filter menus based on WP_Query
body: <p>I have created a filterable products archive page where you can filter by three taxonomies, product_cat, pa_manufacturer &amp; pa_colour. I'm trying to set it up so the dropdown menus are dynamically updated based on the terms of the posts being queried.</p>
  <p>A good example of this is on the ASOS website: <a href="https://www.asos.com/men/ctas/fashion-online-1/cat/?cid=13498&amp;ctaref=hp%7Cmw%7Cprime%7Chero%7C1%7Cedit%7Casosdesign" rel="nofollow noreferrer">https://www.asos.com/men/ctas/fashion-online-1/cat/?cid=13498&amp;ctaref=hp|mw|prime|hero|1|edit|asosdesign</a> where if you select a category, you will see all the other dropdown menus are being updated automatically.</p>
  <p>I've kind of got this working by creating a separate WP_Query to get all the post ids based on all the original query and then using the wp_get_object_terms() for each taxonomy to only get the terms for the post ids selected.</p>
  <p>This works, however the issue i've got now is when a user just selects a single taxonomy, for example, 'cooking' from product category. It will only show 'cooking' in the selected dropdown menu, meaning the user can't select multiple terms unless they deselect the 'cooking' category first.</p>
  <p>If you look at ASOS, this doesn't apply and it allows the user to select multiple categories if they are just searching by a single filter, for example 'category'. However as soon as you filter by 'product type' too, it will then hide the options from the category dropdown.</p>
  <p>Does anyone have a solution to make this work like ASOS?</p>
  <pre><code>/** @global WP_Query $wp_query */
  global $wp_query;
  
  $args         = [];
  $tax_query    = $wp_query-&gt;tax_query-&gt;queries;
  $buffer_query = $wp_query-&gt;query;
  
  $selected_terms = [];
  foreach ( $tax_query as $tq ) {
      if ( $tq === 'AND' || $tq === 'OR' || $tq['taxonomy'] === 'product_visibility' ) {
          continue;
      }
      $selected_terms[ $tq['taxonomy'] ] = $tq;
  }
  
  $buffer_query_args = [
      'nopaging' =&gt; true,
      'fields'   =&gt; 'ids',
  ];
  
  foreach ( $selected_terms as $selected_term ) {
      $buffer_query_args['tax_query'][] = $selected_term;
  }
  
  
  $custom_query          = new WP_Query( array_merge( $buffer_query, $buffer_query_args ) );
  $product_categories    = wp_get_object_terms( $custom_query-&gt;posts, 'product_cat', [ 'parent' =&gt; 0 ] );
  $product_manufacturers = wp_get_object_terms( $custom_query-&gt;posts, 'pa_manufacturer', [ 'parent' =&gt; 0 ] );
  $product_colours       = wp_get_object_terms( $custom_query-&gt;posts, 'pa_colour', [ 'parent' =&gt; 0 ] );
  
  /**
   * Get $product_filters
   *
   * @var $product_filters
   */
  $product_filters = [
      [
          'label'  =&gt; __( 'Category' ),
          'slug'   =&gt; 'product_cat',
          'values' =&gt; $product_categories
      ],
      [
          'label'  =&gt; __( 'Manufacturer' ),
          'slug'   =&gt; 'pa_manufacturer',
          'values' =&gt; $product_manufacturers
      ],
      [
          'label'  =&gt; __( 'Colour' ),
          'slug'   =&gt; 'pa_colour',
          'values' =&gt; $product_colours
      ],
  ];
  ?&gt;
  &lt;div id=&quot;product_filters&quot;&gt;
      &lt;h4 class=&quot;product-filters-label&quot;&gt;&lt;?php _e( 'Filter by' ); ?&gt;:&lt;/h4&gt;
      &lt;form class=&quot;product-filters&quot; method=&quot;GET&quot;&gt;
          &lt;?php foreach ( $product_filters as $product_filter ) : ?&gt;
              &lt;?php
  
              $label  = $product_filter['label'];
              $slug   = $product_filter['slug'];
              $tax    = get_taxonomy( $slug );
              $values = $product_filter['values'];
  
              ?&gt;
              &lt;div class=&quot;product-search-field&quot;&gt;
                  &lt;label for=&quot;product_filter_&lt;?php echo esc_attr( $slug ); ?&gt;&quot;&gt;
                      &lt;span class=&quot;visually-hidden&quot;&gt;&lt;?php echo esc_html( $product_filter['label'] ); ?&gt;&lt;/span&gt;
                      &lt;select
                              data-placeholder=&quot;&lt;?php echo esc_html( $product_filter['label'] ); ?&gt;&quot;
                              name=&quot;&lt;?php echo esc_attr( $slug ); ?&gt;[]&quot;
                              class=&quot;product-ajax-filter&quot;
                              id=&quot;product_filter_&lt;?php echo esc_attr( $slug ); ?&gt;&quot; multiple=&quot;multiple&quot;&gt;
  
                          &lt;?php foreach ( $values as $value ) : ?&gt;
                              &lt;?php
                              $selected = is_array( $_GET[ $slug ] ) &amp;&amp; in_array( $value-&gt;slug, $_GET[ $slug ] );
                              ?&gt;
                              &lt;option value=&quot;&lt;?php echo esc_attr( $value-&gt;slug ); ?&gt;&quot;
                                  &lt;?php echo $selected ? 'selected' : ''; ?&gt;&gt;
                                  &lt;?php echo esc_html( $value-&gt;name ); ?&gt;
                              &lt;/option&gt;
  
                          &lt;?php endforeach; ?&gt;
                      &lt;/select&gt;
                  &lt;/label&gt;
              &lt;/div&gt;
          &lt;?php endforeach; ?&gt;
  
          &lt;noscript&gt;
              &lt;input type=&quot;submit&quot; value=&quot;&lt;?php _e( 'Submit' ); ?&gt;&quot;&gt;
          &lt;/noscript&gt;
      &lt;/form&gt;
  &lt;/div&gt;
  </code></pre>