Mysql query and odd results

时间:2013-03-14 作者:revo

我在这里有一个疑问:

$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 是一个%keyword%$post_type 是一个后类型的slug,奇怪的是当我使用例如。articles 作为帖子类型,结果还包含带有revision 状态和pagepost 类型,似乎if statements 喜欢post_type = \'{$post_type}\'post_status = \'publish\' 根本不起作用!!

我该怎么办?

Update

我做了一个print_r() 对于$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

1 个回复
最合适的回答,由SO网友:david.binda 整理而成

您必须将或零件括在括号内:

$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);
试试这个:

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

结束

相关推荐

这里有什么jQuery冲突吗?

我正在自定义一个名为Accordium, 我决定使用一个名为pbd-ajax-load-posts. 在主题中,当您在存档视图中单击帖子时,手风琴动画将按标准播放。问题是,当你点击Load More posts[波斯语最下方]并像点击帖子一样显示时,插件无法呈现存档中帖子的默认手风琴风格,留下一个固定的打开手风琴。屏幕截图1:h.dropcanvas。com/bf49k/ajax\\u加载。jpg屏幕截图#2:h.dropcanvas。com/5aoza/ajax\\u加载2。在我的网站上,它似乎只显示了