0

I've a query here :

$sql = "SELECT ID,post_title,post_content,post_date,post_type 
FROM {$wpdb->posts} 
WHERE post_content LIKE {$stuff} 
AND post_status = 'publish' 
AND post_type = '{$post_type}' 
AND ID != {$post->ID} 
LIMIT {$limit}";
$results= $wpdb->get_results($sql);

$stuff is a %keyword% and $post_type is a post type slug, the odd thing here is that when I use e.g. articles as post type, results also contains posts with revision status and page or post type and it seems that if statements like post_type = '{$post_type}' and post_status = 'publish' doesn't work at all!!

What should i do?

Update

I did a print_r() for $sql :

SELECT ID,post_title,post_content,post_date,post_type FROM wp_posts WHERE post_content LIKE '%Battery%' OR post_content LIKE '%Watcher%' OR post_content LIKE '%Widget%' OR post_content LIKE '%matches%' OR post_content LIKE '%built%' OR post_content LIKE '%HTC%' OR post_content LIKE '%widgets%' OR post_content LIKE '%Please%' OR post_content LIKE '%check%' OR post_content LIKE '%Thrutu%' OR post_content LIKE '%day%' OR post_content LIKE '%job%' OR post_content LIKE '%app%' OR post_content LIKE '%donate%' OR post_content LIKE '%buying%' AND post_status = 'publish' AND post_type = 'articles' AND ID != 1388 LIMIT 4

4
  • What is a %keyword%? Also, var_dump your $sql and verify that all of the parameters are being set correctly?
    – s_ha_dum
    Commented Mar 14, 2013 at 14:48
  • That, and also you need to have single quotes around the stuff.
    – Otto
    Commented Mar 14, 2013 at 14:53
  • @s_ha_dum Please see the update
    – revo
    Commented Mar 14, 2013 at 15:12
  • @Otto $sql contains it, please see.
    – revo
    Commented Mar 14, 2013 at 15:13

1 Answer 1

0

You have to enclose the OR part to brackets:

$sql = "SELECT ID,post_title,post_content,post_date,post_type 
FROM {$wpdb->posts} 
WHERE (post_content LIKE {$stuff}) 
AND post_status = 'publish' 
AND post_type = '{$post_type}' 
AND ID != {$post->ID} 
LIMIT {$limit}";
$results= $wpdb->get_results($sql);

Try this one:

SELECT ID,post_title,post_content,post_date,post_type FROM wp_posts WHERE (post_content LIKE '%Battery%' OR post_content LIKE '%Watcher%' OR post_content LIKE '%Widget%' OR post_content LIKE '%matches%' OR post_content LIKE '%built%' OR post_content LIKE '%HTC%' OR post_content LIKE '%widgets%' OR post_content LIKE '%Please%' OR post_content LIKE '%check%' OR post_content LIKE '%Thrutu%' OR post_content LIKE '%day%' OR post_content LIKE '%job%' OR post_content LIKE '%app%' OR post_content LIKE '%donate%' OR post_content LIKE '%buying%') AND post_status = 'publish' AND post_type = 'articles' AND ID != 1388 LIMIT 4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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