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'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

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

1 Answer

up vote 0 down vote accepted

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
share|improve this answer
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.