Search code examples
wordpresssearchadvanced-custom-fieldsacfpro

WordPress ACF Search Between two numbers


I need help please, I need to make a search between two number ranges for example I have 21000-23000, 16000-22300, 30000-40000 and I want to find 22300 so the results will be: 21000-23000, 16000-22300 I know that I need to use type BETWEEN but for some reason it show the result only if the minimum number is correct I have two codes 1.

// Filter by price renge      
   if((isset($_REQUEST['min_price']) && $_REQUEST['min_price']!='') && (isset($_REQUEST['max_price']) && $_REQUEST['max_price']!='')){
      
        $filtermeta[] = array(
                'key' => 'minimum_price',
                'value' => array(trim($_REQUEST['min_price']), trim($_REQUEST['max_price'])),
                'compare' => 'BETWEEN',
                'type' => 'numeric'
            );   
        $filtermeta[] = array(
                'key' => 'maximum_price',
                'value' => array(trim($_REQUEST['min_price']), trim($_REQUEST['max_price'])),
                'compare' => 'BETWEEN',
                'type' => 'numeric'
            );   
     
    }
    
    if( !empty($filtermeta) && count($filtermeta)>1 ){
        $arg['meta_query'] = $filtermeta;   
    }

The search doesn't work at all with this code, not sure why and another one that works but incorrect is this one 2.

if(isset($_REQUEST['min_price']) && $_REQUEST['min_price']!=''){
      
        $filtermeta[] = array(
                'key' => 'minimum_price',
                'value' => array(trim($_REQUEST['min_price']), 1000000),
                'compare' => 'BETWEEN',
                'type' => 'numeric'
            );      
    }
    if( !empty($filtermeta) && count($filtermeta)>1 ){
        $arg['meta_query'] = $filtermeta;   
    }

the input search filed:

<div class="form-group"> 
                                      <input type="number" min=0 max="9999999999" name="min_price" value="1"  oninput="validity.valid||(value='0');" id="min_price" class="price-range-field" />
                                    </div>

Can you please help me? I'm very new to PHP don't understand what to change here


Solution

  • Actually, I tried to set max instead of min and it worked

    so I made this:

    if(isset($_REQUEST['max_price']) && $_REQUEST['max_price']!=''){
          
            $filtermeta[] = array(
                    'key' => 'maximum_price',
                    'value' => array(trim($_REQUEST['max_price']), 1000000),
                    'compare' => 'BETWEEN',
                    'type' => 'numeric'
                );      
        }
        if( !empty($filtermeta) && count($filtermeta)>1 ){
            $arg['meta_query'] = $filtermeta;   
        }
    

    and changed the input

    <div class="form-group"> 
    <input type="number" min=0 max="9999999999" name="max_price" value="100"  oninput="validity.valid||(value='1000');" id="max_price" class="price-range-field" />
    </div>
    

    But nut sure if this is a good code, would be happy to hear thoughts