Constructing large PHP query

I’m working with WordPress with custom fields and trying to convert a query which was initially written for pjjtextbase (textfile database) to search a csv file for matching properties for rent/sale.

I understand that to query a custom posts in WP with custom fields I would write something like;

Read More
        $location = preg_replace('/^-|-$|[^-a-zA-Z0-9]/', '', $_GET['location']); 
        $type = preg_replace('/^-|-$|[^-a-zA-Z0-9]/', '', $_GET['type']); 
        $buyrent = preg_replace('/^-|-$|[^-a-zA-Z0-9]/', '', $_GET['buyrent']); 
        $min_val =preg_replace('/[^0-9]/', '', $_GET['min_val']);
        $room_no_min = preg_replace('/[^0-9]/', '', $_GET['room_no_min']); 
        // This replaces characters in the input string if need be for security reasons         

$args =

        array(
            'post_type'  => 'properties',
            'meta_query' => 

            array(
                'key'     => 'wp_location',
                'value'   => $location,
                'compare' => '=',
            )

        );
            $results = new WP_Query($args);

But when I look at the code the programmer wrote

/***** THESE VALUES NEEDED FOR SELECT BOX *****/
$locations = ptb_listUnique($info, 'Property_Location');
$property_type = ptb_listUnique($info, 'Property_Type');
$property_buy_rent = ptb_listUnique($info, 'Buy_Rent');



//check property
$query = Array();
if(!empty($_GET['location'])) { 
   $query[] = "'Property_Location' == '".$_GET['location']."'";
}
if(!empty($_GET['type'])) { 
   $query[] = "'Property_Type' == '".$_GET['type']."'";
}
if(!empty($_GET['buyrent'])) { 
   $query[] = "'Buy_Rent' == '".$_GET['buyrent']."'";
}


//check price
if(empty($_GET['buyrent']) || $_GET['buyrent']=="Buy"){
if(!empty($_GET["min_val_buy"]) && is_numeric($_GET["min_val_buy"])){
     $min_val_buy = $_GET["min_val_buy"];
}
     if(!empty($_GET["max_val_buy"]) && is_numeric($_GET["max_val_buy"])){
     $max_val_buy = $_GET["max_val_buy"];
}

$min_val_buy = min($_GET["min_val_buy"], $_GET["max_val_buy"]);
$max_val_buy = max($_GET["min_val_buy"], $_GET["max_val_buy"]);

$query[] = "'Price_Rent' >= '".$min_val_buy."'";
$query[] = "'Price_Rent' <= '".$max_val_buy."'";

}else{

if(!empty($_GET["min_val_rent"]) && is_numeric($_GET["min_val_rent"])){
     $min_val = $_GET["min_val_rent"];
}
     if(!empty($_GET["max_val_rent"]) && is_numeric($_GET["max_val_rent"])){
     $max_val = $_GET["max_val_rent"];
}

$min_val_rent = min($_GET["min_val_rent"], $_GET["max_val_rent"]);
$max_val_rent = max($_GET["min_val_rent"], $_GET["max_val_rent"]);

$query[] = "'Price_Rent' >= '".$min_val_rent."'";
$query[] = "'Price_Rent' <= '".$max_val_rent."'";

}

//check bedrooms
if(!empty($_GET["room_no_min"]) && is_numeric($_GET["room_no_min"])){
     $room_min_val = $_GET["room_no_min"];
}
if(!empty($_GET["room_no_max"]) && is_numeric($_GET["room_no_max"])){
     $room_max_val = $_GET["room_no_max"];
}

$room_min_val = min($_GET["room_no_min"], $_GET["room_no_max"]);
$room_max_val = max($_GET["room_no_min"], $_GET["room_no_max"]);

$query[] = "'No_Bedrooms' >= '".$room_min_val."'";
$query[] = "'No_Bedrooms' <= '".$room_max_val."'";

//Now build the query to search
$query = implode(' AND ', $query);

It dawned on me that he is not creating multiple queries and then using IMPLODE to combine them but rather creating one whole string then using IMPLODE to add the AND to create the actual query.

The custom fields I have in WP are exactly the same as the large code variable except they are preceded with wp_.

So my question is how do I go about converting his code into a WordPress query?

Related posts