如何使用元查询创建前端后置过滤器

时间:2017-03-25 作者:Alvin Ofori-Adjei

我创建了一个如下图所示的后过滤器,但我在从不同字段选择多个值时遇到了问题。过滤器中的所有字段都是自定义字段。问题是,在管理面板中,一个帖子有一个带有城市Telaviv的选择字段和一个带有value Garden公寓的选择字段属性类型。另一个岗位的城市和物业类型与地面公寓不同。在前端,如果我选择城市领域->特拉维夫,我会得到一个职位(公寓),因为它应该。如果我从属性类型字段->花园和地面公寓中选择,而不选择城市,我将得到两个结果,但如果我选择这两个属性类型公寓和特拉维夫城市,我应该只得到一个公寓,但我仍然得到两个结果。

enter image description here

The code: 您可能会注意到,一些字段值是由变量调用的,我之前只是调用了这些字段,但我没有在这里添加代码。

<form class="select_form advance" id = "filterformid" name="filterform" action="" method="get">
<fieldset>

<div id="stickySearch" style="background: transparent !important;">
  <div class="container">  

        <ul class="nav nav-tabs" data-tabs="tabs" id="search-tab" style="background: transparent !important;">           

<li id="city" class="" name="city">
    <h4 id="apartment_city"><?php echo $filter_city; ?> <span class="caret"></span></h4>
    <ul class="dropdown-list city"> 

        <?php $array_property = get_field_object($filter_city_field);

        $i=0;
        foreach ($array_property[\'choices\'] as $key => $value) {
        $i++;
        if (in_array($key, $_GET[\'city\'])) {

        echo \'<li><input type="checkbox" id="city\'. $i .\'" class="uniquecity" name="city[]" value="\'. $key .\'" checked><label for="city\'. $i .\'">\'.$value.\'</label></li>\';
        } else {
        echo \'<li><input type="checkbox" id="city\'. $i .\'" class="uniquecity" name="city[]" value="\'. $key .\'"><label for="city\'. $i .\'">\'.$value.\'</label></li>\';
        }
    }
        ?>
    </ul>            
</li>



                        <li id="beds" class="" name="rooms">
    <h4 id="bedrooms"><?php echo $filter_rooms; ?> <span class="caret"></span></h4>
    <ul class="dropdown-list beds"> 

        <?php $array_property = get_field_object(\'field_58ab19f3aef68\');

        $i=0;
        foreach ($array_property[\'choices\'] as $key => $value) {
        $i++;
        if($key == $_GET[\'rooms\'] && !empty($_GET[\'rooms\'])){
        echo \'<li><input type="checkbox" id="bed\'. $i .\'" class="uniqueBed" name="rooms" value="\'. $key .\'" checked><label for="bed\'. $i .\'">\'.$value.\'</label></li>\';
        } else {
        echo \'<li><input type="checkbox" id="bed\'. $i .\'" class="uniqueBed" name="rooms" value="\'. $key .\'"><label for="bed\'. $i .\'">\'.$value.\'</label></li>\';
        }
    }
        ?>
    </ul>            
</li>


<li id="floors" class="" name="floors">
    <h4 href="#" id="apartment_floors"><?php echo $filter_floors; ?> <span class="caret"></span></h4>
    <ul class="dropdown-list floors"> 

        <?php $array_property = get_field_object(\'field_58ab1a34aef69\');
        $i=0;
        foreach ($array_property[\'choices\'] as $key => $value) {
            $i++;
        if($key == $_GET[\'floors\'] && !empty($_GET[\'floors\'])){
        echo \'<li><input type="checkbox" id="floors\'. $i .\'" class="uniquefloors" name="floors" value="\'. $key .\'" checked><label for="floors\'. $i .\'">\'.$value.\'</label></li>\';
        } else {
        echo \'<li><input type="checkbox" id="floors\'. $i .\'" class="uniquefloors" name="floors" value="\'. $key .\'"><label for="floors\'. $i .\'">\'.$value.\'</label></li>\';
        }
    }
        ?>
    </ul>            
</li>



<li id="property_exposure" name="property">
    <h4 href="#" id="exposure"><?php echo $filter_exposure; ?> <span class="caret"></span></h4>
    <ul class="dropdown-list exposure"> 

    <?php 

    $array_property = get_field_object($filter_exposure_field);
    $i=0;
    foreach ($array_property[\'choices\'] as $key => $value) {
    $i++;
    if (in_array($key, $_GET[\'property\'])) {                                    
    echo \'<li><input type="checkbox" id="exposure\' . $i. \'" class="uniqueexposure" name="property[]" value="\'.$key.\'" checked><label for="exposure\'. $i .\'">\'.$value.\'</label>\';
    } else {
    echo \'<li><input type="checkbox" id="exposure\' . $i. \'" class="uniqueexposure" name="property[]" value="\'.$key.\'"><label for="exposure\'. $i .\'">\'.$value.\'</label>\';
    }   }
    ?>




    </ul>            
</li>

<li id="price">
  <h4 href="#"><?php echo $filter_price; ?> <span class="caret"></span></h4>
  <ul class="dropdown-list price">
    <li>

        <?php if($_GET[\'price_from\'] != \'0\' && empty($_GET[\'price_from\'])){ ?>
        <?php echo $filter_from; ?>
        <input placeholder="<?php echo $filter_min; ?>" type="text" name="price_from" id="min-price" value="">
        <?php } else { ?>
        <input placeholder="<?php echo $filter_min; ?>" type="text" name="price_from" id="min-price" value="<?php echo $_GET[\'price_from\']; ?>">
        <?php } ?></li>
        <li>

      <?php echo $filter_to; ?> 

      <?php if(empty($_GET[\'price_to\'])){ ?>
        <input placeholder="<?php echo $filter_max; ?>" type="text" name="price_to" id="max-price" value="">
        <?php } else { ?>
        <input placeholder="<?php echo $filter_max; ?>" type="text" name="price_to" id="max-price" value="<?php echo $_GET[\'price_to\']; ?>">
        <?php } ?></li>



   </li>
  </ul>
</li>



<li id="property_type" class="" name="property_type">
    <h4 href="#" id="apartment_floors"><?php echo $filter_property_type; ?> <span class="caret"></span></h4>
    <ul class="dropdown-list property_type"> 

        <?php $array_property1 = get_field_object($filter_property_field);
        $i=0;

        foreach ($array_property1[\'choices\'] as $key => $value) {
            $i++;


        if (in_array($key, $_GET[\'property_type\'])) {
        echo \'<li><input type="checkbox" id="property\'. $i . \'" class="uniqueproperty" name="property_type[]" value="\'. $key .\'" checked><label for="property\'. $i .\'">\'.$value.\'</label></li>\';
        } else {
        echo \'<li><input type="checkbox" id="property\'. $i .\'" class="uniqueproperty" name="property_type[]" value="\'. $key .\'"><label for="property\'. $i .\'">\'.$value.\'</label></li>\';
        }
    }
        ?>
    </ul>            
</li>



<?php 
$array_includes = array(array(
                                \'label\' => \'Parking\',
                                \'acf_name\' => \'parking\'
                                    )
                                    );
                                ?>


<li id="parking" class="" name="parking">


                                <?php foreach ($array_includes as $value) { ?>      
                                        <?php if( !empty($_GET[\'advance\']) && in_array($value[\'acf_name\'], $_GET[\'advance\']) ) { ?>
                                        <h4 href="#" id="apartment_floors"><?php echo $filter_parking; ?> <input type="checkbox" checked id="<?php echo $value[\'acf_name\'] ?>" name="advance[]" value="<?php echo $value[\'acf_name\'] ?>"></h4>

                                        <?php } else { ?>
                                            <h4 href="#" id="apartment_floors"><?php echo $filter_parking; ?> <input type="checkbox" id="<?php echo $value[\'acf_name\'] ?>" name="advance[]" value="<?php echo $value[\'acf_name\'] ?>"></span></h4>
                                        <?php } ?> 


                                <?php } ?>
                                </li>






<li id="meters1">
  <h4 href="#"><?php echo $filter_meters; ?> <span class="caret"></span></h4>
  <ul class="dropdown-list meters">
    <li>
<?php echo $filter_from; ?>
        <?php if($_GET[\'meters_from\'] != \'0\' && empty($_GET[\'meters_from\'])){ ?>
                                    <input placeholder="<?php echo $filter_min; ?>" type="text" name="meters_from" id="min-meters" value="">
                                <?php } else { ?>
                                    <input placeholder="<?php echo $filter_min; ?>" type="text" name="meters_from" id="min-meters" value="<?php echo $_GET[\'meters_from\']; ?>">
                                <?php } ?></li>
                                <li>

       <?php echo $filter_to;?>

      <?php if(empty($_GET[\'meters_to\'])){ ?>
                                    <input placeholder="<?php echo $filter_max; ?>" type="text" name="meters_to" id="max-meters" value="">
                                <?php } else { ?>
                                    <input placeholder="<?php echo $filter_max; ?>" type="text" name="meters_to" id="max-meters" value="<?php echo $_GET[\'meters_to\']; ?>" >
                                <?php } ?></li>



   </li>
  </ul>
</li>
<?php $queried_object_id = $wp_query->queried_object->term_id; ?>

<input type="hidden" name="termid" value="Residential Sale" />

<input type="submit" value="<?php echo $filter_search; ?>">
<div class="advance_block">
<?php $location =  $_GET[\'termid\'];
        $args = array(
        \'post_type\'  => \'home_listings\',
        \'posts_per_page\' => -1,
        \'tax_query\' => array(
            array(
                \'taxonomy\' => \'apartment_categories\',
                \'field\'    => \'term_id\',
                \'terms\'    => $queried_object_id
            ),
        ),
        \'meta_query\' => array(
            \'relation\' => \'AND\'

        )

    );
    if(!empty($_GET[\'advance\'])) {
        foreach($_GET[\'advance\'] as $check) {
            $args[\'meta_query\'][] = array(
                \'key\' => $check,
                \'value\' => \'1\',
                \'compare\' => \'LIKE\'
            );
        }
    }
    foreach ($_GET[\'city\'] as $city_single){
    if(!empty($_GET[\'city\']) && $_GET[\'city\'] != \'all\') {
        $args[\'meta_query\'][] = array(
            \'key\' => \'city\',
            \'value\' => $city_single,
            \'compare\' => \'LIKE\'
        );
    }
}
    if(!empty($_GET[\'neighborhood\']) && $_GET[\'neighborhood\'] != \'all\') {
        $args[\'meta_query\'][] = array(
            \'key\' => \'select_neighbourhood\',
            \'value\' => $_GET[\'neighborhood\'],
            \'compare\' => \'LIKE\'
        );
    }
    if ( !empty($_GET[\'price_from\']) || !empty($_GET[\'price_to\']) ) {
        if($_GET[\'price_from\'] != \'all\'){
            $from = (int)$_GET[\'price_from\'];
        } else {
            $from = 0;
        }
        if($_GET[\'price_to\'] != \'all\'){
            $to = (int)$_GET[\'price_to\'];
        } else {
            $to = 0;
        }
            $args[\'meta_query\'][] = array(
            \'key\' => \'price\',
            \'value\' => array($from,$to),
            \'type\' => \'numeric\',
            \'compare\' => \'BETWEEN\'
        );
    }
    if ( !empty($_GET[\'meters_from\']) || !empty($_GET[\'meters_to\']) ) {
        if($_GET[\'meters_from\'] != \'all\'){
            $from = (int)$_GET[\'meters_from\'];
        } else {
            $from = 0;
        }
        if($_GET[\'meters_to\'] != \'all\'){
            $to = (int)$_GET[\'meters_to\'];
        } else {
            $to = 0;
        }
        $args[\'meta_query\'][] = array(
            \'key\' => \'square_feet\',
            \'value\' => array($from,$to),
            \'type\' => \'numeric\',
            \'compare\' => \'BETWEEN\'
        );
    }
    foreach ($_GET[\'property\'] as $exposure_single){
    if(!empty($_GET[\'property\']) && $_GET[\'property\'] != \'all\') {
        $args[\'meta_query\'][] = array(
            \'key\' => \'exposure\',
            \'value\' => $exposure_single,
            \'compare\' => \'LIKE\'
        );
    }
}

        foreach ($_GET[\'property_type\'] as $property_single){
            if(!empty($_GET[\'property_type\']) && $_GET[\'property_type\'] != \'all\') {
        $args[\'meta_query\'][] = array(
            \'key\' => \'property_type\',
            \'value\' => $property_single,
            \'compare\' => \'LIKE\'
        );
        }
    }

    if(!empty($_GET[\'rooms\']) && $_GET[\'rooms\'] != \'all\') {
        $args[\'meta_query\'][] = array(
            \'key\' => \'number_of_rooms\',
            \'value\' => $_GET[\'rooms\'],
            \'compare\' => \'LIKE\'
        );
    }
    if(!empty($_GET[\'floors\']) && $_GET[\'floors\'] != \'all\') {
        $args[\'meta_query\'][] = array(
            \'key\' => \'floors\',
            \'value\' => $_GET[\'floors\'],
            \'compare\' => \'LIKE\'
        );
    }
    // Keep query string vars intact
    foreach ( $_GET as $key => $value ) {

        if ( 
            \'rooms\' === $key || 
            \'floors\' === $key || 
            \'property_type\' === $key ||
            \'submit\' === $key || 
            \'advance\' === $key ||
            \'city\' === $key ||
            \'neighborhood\' === $key ||
            \'property\' === $key ||
            \'price_from\' === $key ||
            \'price_to\' === $key ||
            \'meters_from\' === $key ||
            \'meters_to\' === $key
        ) {
            continue;
        }
        if ( is_array( $val ) ) {
            foreach( $val as $innerVal ) {
                echo \'<input type="hidden" name="\' . esc_attr( $key ) . \'[]" value="\' . esc_attr( $innerVal ) . \'" />\';

            }
        } else {
            echo \'<input type="hidden" name="\' . esc_attr( $key ) . \'" value="\' . esc_attr( $val ) . \'" />\';
        }
    }
    $wp_qunew WP_Query($args);
                                ?>

1 个回复
SO网友:Aftab

请点击此链接

https://aftabablog.wordpress.com/2016/06/16/how-to-create-a-custom-filter-in-wp/

希望这能让您了解如何创建基于元查询的自定义过滤器

相关推荐

是否可以取消对特定帖子类型的POSTS_PER_PAGE限制?

我想知道我是否可以取消特定帖子类型的posts\\u per\\u页面限制。在存档中。php页面我显示不同的帖子类型,对于特定的“出版物”帖子类型,我想显示所有帖子。我如何在不影响传统“post”类型的情况下实现这一点?