PHP query posts array

dstratford used Ask the Experts™
I have a search form which brings in 3 variables. The 3 variables correspond to custom fields in each post of a specific category. How can i get this query to show results matching ALL variables, instead of either of the three?


$type = $_POST["type"];
$price = $_POST["price"];
$location = $_POST["location"];

$args = array(
   'cat'                 => 16,
   'meta_value'   => $type,
   'meta_value'   => $price,
   'meta_value'   => $location,


Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2009
Hi dstratford,

I'm not familiar with WordPress, but looked around a bit. The problem with the custom fields is that the query_posts function does not support multiple custom fields. It only looks up meta_value of the passed array and therefore ignores the other two entries.

You will need to create a custom query yourself.

Take a look at this page with examples of how to create custom queries:

Hope that helps.
Richard DavisSenior Web Developer
If you have any familiarity with SQL then you should be able to forge a query that meets your requirements as wuff1uk had mentioned.

That query would be similar to this;
$safe_cat_id = mysql_real_escape_string($type = $_POST["cat_id"]);
$safe_type = mysql_real_escape_string($type = $_POST["type"]);
$safe_price = mysql_real_escape_string($type = $_POST["price"]);
$safe_location = mysql_real_escape_string($type = $_POST["location"]);

$sql = "SELECT fields... FROM some_table
WHERE cat_id = $safe_cat_id
AND type LIKE '%$safe_type%' AND
AND price = '$safe_price' AND
AND (location LIKE '%$safe_loc' OR location LIKE '%$safe_loc%' OR location LIKE '$safe_loc%'

Hope this helped. :)



thank you very much for your comments - i'm getting really close! f i don't figure it out from here i may repost question with better details

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial