任何人都可以帮助我优化 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
它使我的网络空间(服务器)崩溃了。但为什么呢?
顺便说一句:此查询仅供管理员使用。
最佳答案
(已更新)你能尝试一下吗? 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
关于php - WordPress 自定义 SQL-Query,太重了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26375665/