Page 1 of 1

Quick variable and query help

Posted: Fri Oct 02, 2009 4:34 am
by stevebluck
Hello,

please take a moment to look at my code

Code: Select all

<?php
 
    if ($location != '')
    { 
        $loc = "AND pm.meta_value = '$location'";
    }
    
    if ($beds != '')
    { 
        $bed = "AND pm.meta_value = '$beds'";
    }
    
    if ($price != '')
    { 
        $pri = "AND pm.meta_value = '$price'";
    }
    
    $query = "SELECT the_posts.*
                     FROM wp_posts the_posts, wp_postmeta pm
                     WHERE the_posts.id = pm.post_id AND
                    
                        the_posts.id IN (
                            SELECT xposts.id
                            FROM wp_posts xposts, wp_term_relationships catspost
                            WHERE catspost.object_id = xposts.id
                            AND catspost.term_taxonomy_id IN ($cat_id)
                    
                     ) AND
                     ((
                      pm.meta_key = 'state_value'
                      I WANT TO PUT THE $LOC VARIABLE HERE
                     ) OR
                     (
                      pm.meta_key = 'price_value'
                      I WANT TO PUT THE $PRI VARIABLE HERE
                     ) OR
                     (
                      pm.meta_key = 'beds_value'
                      I WANT TO PUT THE $BED VARIABLE HERE
                     ))
                     GROUP BY the_posts.id;
                     ";
                            
    $pageposts = $wpdb->get_results($query, OBJECT);
    
    
?>
I simply would like to know how I put these variables inside the SQL, i'm not sure on the syntax. I have tried this:

Code: Select all

$query = "SELECT the_posts.*
                     FROM wp_posts the_posts, wp_postmeta pm
                     WHERE the_posts.id = pm.post_id AND
                    
                        the_posts.id IN (
                            SELECT xposts.id
                            FROM wp_posts xposts, wp_term_relationships catspost
                            WHERE catspost.object_id = xposts.id
                            AND catspost.term_taxonomy_id IN ($cat_id)
                    
                     ) AND
                     ((
                      pm.meta_key = 'state_value'
                      ". $loc ."
                     ) OR
                     (
                      pm.meta_key = 'price_value'
                      ". $pri ."
                     ) OR
                     (
                     pm.meta_key = 'beds_value'
                     ". bed ."
                     ))
                     GROUP BY the_posts.id;
                     ";
But with no luck. Please help!

Many thanks.

Re: Quick variable and query help

Posted: Fri Oct 02, 2009 11:49 pm
by Robert07
Have you tried to echo the $query after defining it so you can make sure it is created the way you want it to be? If you echo it, then run that query in phpmyadmin or the mysql command line, what is the result?