doutan1875 2018-07-09 21:53
浏览 47

'key'meta_query无法正常工作自定义高级搜索(WordPress)

Been trying for hours now and have almost got to breaking point. Tried so many different things but can't seem to get my custom search bar to query results by a pre-defined select price range. I am using Advanced Custom Fields to add the meta tag 'investmentprice' to my custom post type called 'investments'. I feel like it's to do with WordPress not picking up the correct 'key' in my meta_query, even though I have changed this numerous times now.

Here's the code for the config bar:

<div class="config_bar cf">
<form method="get" role="search" action="<?php echo esc_url( home_url( '/' ) ); ?>">
    <input type="hidden" name="s" value="">
    <input type="hidden" name="post_type" value="investments" />

    <div class="col">
        <?php 
            $taxonomy = 'type';
            $args = array( 'orderby' => 'name', 
                'order'             => 'ASC',
                'hide_empty'        => true,
            );      
            $tax_terms = get_terms($taxonomy, $args);
        ?>
        <span class="label_inner">Investment Type</span>
        <select name="type" id="type" class="postform standard">
            <option value="" selected="selected">All Investment Types </option>
            <?php if($tax_terms): ?>
                <?php foreach ($tax_terms as $tax_term): ?>
                    <?php $title = $tax_term->name;
                     ?>
                    <option value="<?php echo $tax_term->slug; ?>"><?php echo $title; ?></option>
                <?php endforeach; ?>
            <?php endif; ?>             
        </select>
    </div><!-- col -->
    <div class="col">
        <span class="label_inner"><?php _e('Select Country','opencloud');?></span>
        <select class="postform standard country " name="country" id="country">
           <option value=""><?php _e('All Countries','opencloud');?></option>
           <?php
           // Display only parents here .
           $terms = get_terms( array(
                // Put your taxonomy name  here.
                'taxonomy' => 'location',
                'parent' => 0, 
                'hide_empty' => false
            ) );

            foreach ($terms as $term){?>
                <!-- We are going to send value for $_POST and data-makeId's TERM_ID for ajax request -->
                <option value="<?php echo $term->slug;?>" data-countryId="<?php echo $term->term_id ?>"><?php echo $term->name;?></option> 
            <?php
            wp_reset_query(); // if you're not in your main loop! otherwise you can skip this
            } ?>
        </select>    
    </div><!-- col -->
    <script type="text/javascript">
        $( document ).ready(function() {
            $('#country').change(function(){
              var $mainCat= $(this).find(':selected').attr('data-countryId');
              if ($mainCat != '0' ){
                // call ajax
                $("#city").empty();
                $.ajax
                (
                  {
                    url:"<?php bloginfo('wpurl'); ?>/wp-admin/admin-ajax.php",     
                    type:'POST',
                    data:'action=get_city_lists_ajax&main_catid=' + $mainCat,
                    beforeSend:function()
                    {
                    },
                    success:function(results)
                    {
                      $("#loading_bar").hide();
                      $("#city").removeAttr("disabled").trigger('change.select2');       
                      $("#city").append(results).trigger('change.select2'); 
                    }
                  }
                );   
              }

            });
        });    
    </script>
    <div class="col">
        <span class="label_inner"><?php _e('Select City','opencloud');?></span>
        <select class="postform standard city " name="city" id="city" disabled>
           <option value="<?php echo $term->slug;?>"><?php _e('All Cities','opencloud');?></option>
        </select>    
    </div><!-- col -->
    <div class="col">
        <span class="label_inner">Select Price</span>
        <select class="postform standard price" name="price" id="price">
            <option value="">All Prices</option>
           <option value="500-1000">£500-£1000</option>
           <option value="1000-1500">£1000-£1500</option>
           <option value="1500-2000">£1500-£2000</option>
           <option value="2000-5000">£1500-£2000</option>
           <option value="5000-10000">£5000-£10,000</option>
        </select>    
    </div><!-- col -->
    <div class="col">
        <button class="search_submit" type="submit">Search</button>
    </div><!-- col -->
</form>

And here's my advanced search query function:

function advanced_search_query( $query ) {

if ( isset( $_REQUEST['search'] ) && $_REQUEST['search'] == 'advanced' && !is_admin() && $query->is_search && $query->is_main_query() ) {

// limit query for custom post type
    $query->set( 'post_type', 'investments' );

// Get query strings from URL and store the min a variable
    $_type = $_GET['type'] != '' ? $_GET['type'] : '';
    $_country = $_GET['country'] != '' ? $_GET['country'] : '';
    $_city = $_GET['city'] != '' ? $_GET['city'] : '';
    $_price = $_GET['price'] != '' ? $_GET['price'] : '';

  if( $_price != '' ) {
    $metaquery = array(
        array(
            'key' => 'investmentprice',
            'terms' => $_price,
            'compare' => 'BETWEEN'
        )
    );
    $query->set( 'meta_query', $metaquery );
  } 

// if type is not empty limit the taxonomy to the specified
  if( $_type != '' ) {
    $taxquery = array(
        array(
            'taxonomy' => 'type',
            'field' => 'slug',
            'terms' => $_type,
            'operator'=> 'IN'
        )
    );
    $query->set( 'tax_query', $taxquery );
  }

// if country is not empty limit the taxonomy to the specified
  if( $_country != '' ) {
    $taxquery = array(
        array(
            'taxonomy' => 'country',
            'field' => 'slug',
            'terms' => $_country,
            'operator'=> 'IN'
        )
    );
    $query->set( 'tax_query', $taxquery );
  } 

// if city is not empty limit the taxonomy to the specified
  if( $_city != '' ) {
    $taxquery = array(
        array(
            'taxonomy' => 'city',
            'field' => 'slug',
            'terms' => $_city,
            'operator'=> 'IN'
        )
    );
    $query->set( 'tax_query', $taxquery );
  }

  return; // always return
}

}

Also, here's a screenshot of the Advanced Custom Field: Advanced Custom Field (investmentprice)

  • 写回答

1条回答 默认 最新

  • douzhong8856 2018-07-11 16:04
    关注

    Unless there are any other issues, this should be a small fix. You are using the array key 'terms' instead of 'value' in your meta query. According to the WordPress Codex, a nested array of args for a meta query should use value, like this:

    $metaquery = array(
        array(
            'key' => 'investmentprice',
            'value' => $_price,
            'compare' => 'BETWEEN'
        )
    );
    $query->set( 'meta_query', $metaquery );    
    

    Hope that helps..

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?