Dropdown Categories used in form shows no results when all is selected

I’m using the wp_dropdown_categories twice in a search form, once for the categories and second for a custom taxonomy. When selecting the ‘All” option no results are showing in the results page. The term_id of all categories is ‘0’ as it is showing in the URL. When selecting a category AND a custom taxonomy from the dropdown it works fine.

Below is my query and my dropdowns:

if ( isset($_GET['cat']) && isset($_GET['manufacturer']) ) {

                $tax_query = array( 'relation' => 'AND' );
                array_push($tax_query,
                    array(
                        'taxonomy'  => 'manufacturers',
                        'field'     => 'term_id',
                        'terms'     => $_GET['manufacturer'],
                    ),
                    array(
                        'taxonomy'  => 'category',
                        'field'     => 'term_id',
                        'terms'     => $_GET['cat'],
                    )
                );
            };

            if ( ( $_GET['cat'] == 0 ) || ( $_GET['manufacturer'] == 0 ) ) {
                $tax_query = ''
            };


                $query_args = array(
                    'post_type'         => 'yacht',
                    'meta_or_tax'       => true,
                    'tax_query'         => $tax_query,
                    'posts_per_page'    => -1,
                    'meta_query'        => array(
                        'relation'      => 'AND',
                        array(
                         'key'          => 'yachts_loa_length_round',
                         'value'        => array($_GET['min_length'], $_GET['max_length']),
                         'compare'      => 'BETWEEN',
                         'type'         => 'NUMERIC',
                        ),
                        array(
                         'key'          => 'yachts_price',
                         'value'        => array($_GET['min_price'], $_GET['max_price']),
                         'compare'      => 'BETWEEN',
                         'type'         => 'NUMERIC',
                        ),
                        array(
                         'key'          => 'yachts_year',
                         'value'        => array($_GET['min_year'], $_GET['max_year']),
                         'compare'      => 'BETWEEN',
                         'type'         => 'NUMERIC',
                        )
                    )
                );

                $yacht_query = new WP_Query( $query_args );

              $args = array(

                'orderby'            => 'menu_order',
                'show_option_all'    => pll__('All '),
                'order'              => 'ASC',
                'hide_empty'         => 0,
                'exclude'            => -1,
                'name'               => 'cat',
                'class'              => 'form-select',
                'taxonomy'           => 'category',
              );

              wp_dropdown_categories( $args ); ?>

              <?php

              $args = array(

                'orderby'            => 'menu_order',
                'show_option_all'    => pll__('All'),
                'order'              => 'ASC',
                'hide_empty'         => 0,
                'exclude'            => -1,
                'name'               => 'manufacturer',
                'class'              => 'form-select',
                'taxonomy'           => 'manufacturers',
              );

              wp_dropdown_categories( $args ); ?>

Solutions Collecting From Web of "Dropdown Categories used in form shows no results when all is selected"

0 isn’t a valid term ID in a tax query. Don’t add the category tax query parameters if the value is 0, which is equivalent to ignoring category. If you want only posts that are categorized (a category must be assigned, but it can be any category), then use the EXISTS operator.

$tax_query = '';

if( isset($_GET['cat']) && 0 != $_GET['cat'] ) {
    $tax_query[] = array(
        'taxonomy'  => 'category',
        'field'     => 'term_id',
        'terms'     => $_GET['cat']
    );
}

if( isset($_GET['manufacturer']) && 0 != $_GET['manufacturer'] ) {
    $tax_query[] = array(
        'taxonomy'  => 'manufacturers',
        'field'     => 'term_id',
        'terms'     => $_GET['manufacturers']
    );
}

// only add relation if both are set and non-zero
if( isset($_GET['manufacturer']) && 0 != $_GET['manufacturer'] && isset($_GET['cat']) && 0 != $_GET['cat'] ) {                
    $tax_query['relation'] = 'AND';
}

// query args for all queries
$query_args = array(
    'post_type' => 'yacht',
    // other args...
);

// add tax query if it isn't empty
if( !empty( $tax_query ) ){
    $query_args['tax_query'] = $tax_query;
}

$yacht_query = new WP_Query( $query_args );