Support

Account

Home Forums General Issues Visitor to Query Between 2 Numbers Reply To: Visitor to Query Between 2 Numbers

  • @hube2, A huge thanks. That got it working.

    Here’s my full function that may help other people in the future.

    
    function my_pre_get_posts( $query ) {
    	
    	// do not modify queries in the admin
    	if( is_admin() ) {
    		return $query;	
    	}
        
        // allow the url to alter the query
        if( isset($_GET['min_height']) ) {
    
            // Load min_height and max_height as veriables
            $min_height = $_GET['min_height'];
            if ($_GET['max_height'] !='') {
                $max_height = $_GET['max_height'];
            } else {
                $max_height = 9999999;
            }
    
            // Query
            $meta_query = array(
              array(
                'key' => 'mws_height',
                'value' => array($min_height, $max_height),
                'compare' => 'BETWEEN',
                'type' => 'NUMERIC'
              )
            );
            
            $query->set('meta_query', $meta_query);
            // Order by height
    		$query->set('orderby', 'meta_value_num');
    		$query->set('meta_key', 'mws_height');	 
    		$query->set('order', 'ASC'); 
        } 	
    	// return
    	return $query;
    }
    
    add_action('pre_get_posts', 'my_pre_get_posts');