Wordpress自定义SQL-Query,太重了?

时间:2014-10-15 06:14:28

标签: php mysql sql wordpress inner-join

任何人都可以帮我在wordpress中优化我的查询吗?

PHP代码

$serSlugSearchWord = seoUrl($post->post_title);
$serSlugSearchWord_obj = explode("-", $serSlugSearchWord);
foreach ($serSlugSearchWord_obj as $item )
{
    $queryStringKeyWords .= " if( INSTR(post_name, '$item')>0 ,1,0) +";
}

$queryStringKeyWords = preg_replace("/\+$/", "", $queryStringKeyWords);
$theQuery = "Select
$queryStringKeyWords 
as my_rate,
`wp_posts`.*  
from `wp_posts`
WHERE (`post_status` = 'publish' )
AND `post_type` = 'post'
order by my_rate desc limit 0, 10";
global $wpdb;
$wpdb->get_row($theQuery, 'ARRAY_N');

查询看起来像这样(效果很好)

Select if( INSTR(post_name, 'serengeti')>0 ,1,0) + if( INSTR(post_name, 'adventure')>0 ,1,0) as my_rate, `wp_posts`.* from `wp_posts` 
WHERE (`post_status` = 'publish' ) 
AND `post_type` = 'post' 
order by my_rate desc limit 0, 10

现在我只想要类别1和5的帖子

INNER JOIN wp_term_relationships
ON wp_term_relationships.object_id = ID
AND wp_term_relationships.term_taxonomy_id = 1
OR wp_term_relationships.term_taxonomy_id = 5

但是当我将这些行添加到代码

Select if( INSTR(post_name, 'serengeti')>0 ,1,0) + if( INSTR(post_name, 'adventure')>0 ,1,0) as my_rate, `wp_posts`.* from `wp_posts` 
INNER JOIN wp_term_relationships
ON wp_term_relationships.object_id = ID
AND wp_term_relationships.term_taxonomy_id = 1
OR wp_term_relationships.term_taxonomy_id = 5
WHERE (`post_status` = 'publish' ) 
AND `post_type` = 'post' 
order by my_rate desc limit 0, 10

它崩溃了我的网站空间(服务器)。但为什么呢?

顺便说一句:此查询仅适用于管理员。

1 个答案:

答案 0 :(得分:1)

(更新)你能试试吗? OR条件放在括号中。

Select if( INSTR(post_name, 'serengeti')>0 ,1,0) + if( INSTR(post_name, 'adventure')>0 ,1,0) as my_rate, `wp_posts`.* from `wp_posts` 
INNER JOIN wp_term_relationships
ON wp_term_relationships.object_id = ID
WHERE (`post_status` = 'publish' ) 
AND `post_type` = 'post' 
AND wp_term_relationships.term_taxonomy_id in (1,5)
order by my_rate desc limit 0, 10