Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using the $wpdb class to do a custom SQL query but I am getting weird results. Basically this is what my SQL query is supposed to do

  1. Retrieve the titles of post_types 'question' only if they are
  2. published within the last 7 days
  3. have a taxonomy of 'question_tag'
  4. have the terms 'another-sample' or 'sample'

The problem is the number 2. The query seems to be returning questions that are dated later than 7 days. To illustrate this I am also retrieving the post_date. As you can see in the below result set. the question titled 'lkddfs' is more than 7 days old. Help?

function rs_send_email_to_interested_users() {
    global $wpdb;
    $wpdb->show_errors();
    $sql ="SELECT DISTINCT $wpdb->posts.post_title,  $wpdb->posts.post_date FROM $wpdb->posts
    INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
    INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    INNER JOIN $wpdb->terms ON ($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)
    WHERE 1=1 AND
    $wpdb->term_taxonomy.taxonomy = 'question_tag' AND
    $wpdb->posts.post_type = 'question' AND
    $wpdb->posts.post_status = 'publish' AND
    $wpdb->terms.slug = 'another-sample' OR $wpdb->terms.slug = 'sample' AND
    //the date logic doesn't seem to qork quite right
    $wpdb->posts.post_date > DATE_SUB( NOW(), INTERVAL 7 DAY)";
    $question_titles = $wpdb->get_results( $sql, ARRAY_N );
    return $question_titles;
}

And here is my result:

Array
(
    [0] => Array
        (   
            [0] => lkddfs
            [1] => 2013-03-03 03:05:57 // This question is more than 7 days old!
        )

    [1] => Array
        (
            [0] => Good tactical games
            [1] => 2013-03-24 04:48:04
        )

    [2] => Array
        (
            [0] => my recent question
            [1] => 2013-03-24 14:58:25
        )

    [3] => Array
        (
            [0] => romans question
            [1] => 2013-03-26 21:18:00
        )

)
share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

This may be your problem:

$wpdb->terms.slug = 'another-sample' OR $wpdb->terms.slug = 'sample' AND
//the date logic doesn't seem to qork quite right
$wpdb->posts.post_date > DATE_SUB( NOW(), INTERVAL 7 DAY)";

Your OR operator might be confusing the query. Try using parentheses:

($wpdb->terms.slug = 'another-sample' OR $wpdb->terms.slug = 'sample') AND
//the date logic doesn't seem to qork quite right
$wpdb->posts.post_date > DATE_SUB( NOW(), INTERVAL 7 DAY)";
share|improve this answer
    
Yup that was the problem! Thank you so much. –  Roman Epicnerd Sharf Mar 26 '13 at 23:48
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.