选择全部时,表单中使用的下拉类别不显示任何结果

时间:2015-10-17 作者:vagelis

我在搜索表单中使用了两次wp\\u下拉菜单\\u categories,一次用于类别,另一次用于自定义分类法。选择“全部”选项时,结果页中不会显示任何结果。所有类别的term\\u id为“0”,正如在URL中显示的那样。从下拉列表中选择类别和自定义分类法时,效果良好。

下面是我的查询和下拉列表:

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 ); ?>

1 个回复
最合适的回答,由SO网友:Milo 整理而成

0不是税务查询中的有效术语ID。如果值为0,则不添加类别税查询参数,这相当于忽略类别。如果您只想要分类的帖子(必须指定一个类别,但它可以是任何类别),那么使用EXISTS 操作人员

$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 );