내 Wordpress (MySQL) 데이터베이스에서 이미지의 설명 및 대체 텍스트를 검색하는이 검색 코드가 있습니다. 그러나 좀 더 "더 나은"검색이 필요하고 어떻게 향상시킬 수 있는지 궁금합니다.이 검색 코드는 어떻게 개선 될 수 있습니까?
이예 : 새 이미지의 일반적인 설명은 다음과 같습니다 "비행 큰 새 블루 헤론 대형 그레이트 블루 비행 비행"
는 현재 아래 코드는 그 단어의 단독 사용하여 일치하는 이미지에 대한 설명을 찾을 수 있습니다 . 그러나 검색 단어로 "그레이트 블루 헤론"을 결합하면 나는 아무 것도 얻지 못합니다.
일부 SQL 마법사를 사용하면 코드에서 쿼리 메서드를 수정하여 검색 기능을 확장 할 수있는 비교적 쉬운 방법이 있다고 생각합니다. 당신은 내가 순차적 인 검색을 의미 선두 %
시작
class WPSSearchImages
{
function __construct() {
//NOP
}
//Searches all gallery photos using a keyword and returns an array of results containing post IDs (ie, image IDs)
static function perform_gallery_keyword_search($keyword)
{
//Handles the keyword search of galleries and photos
global $wpdb;
//Clear the previous search result array if it exists
WPSSession::drop("last_search_results");
WPSSession::drop("last_search_term");
//Perform queries and collect the results
//1) Search image description and alt text
$subquery1 = "$wpdb->posts.post_content LIKE '%$keyword%'"; //Takes care of the post content part
$subquery2 = "$wpdb->postmeta.meta_key = '_wp_attachment_image_alt' AND $wpdb->postmeta.meta_value LIKE '%$keyword%'"; //Takes care of image alt text part
$main_subquery = $subquery1 . " OR " . $subquery2;
$querystr = "
SELECT DISTINCT $wpdb->postmeta.post_id
FROM $wpdb->posts, $wpdb->postmeta
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->postmeta.meta_key = '_wpps_gallery_id'
AND $wpdb->posts.post_type = 'attachment'
AND $main_subquery
ORDER BY $wpdb->postmeta.post_id ASC
";
$results1 = $wpdb->get_results($querystr, ARRAY_A);
//2) Search image caption/excerpt
$subquery1 = "$wpdb->posts.post_excerpt LIKE '%$keyword%'"; //Takes care of the image caption part
$querystr = "
SELECT DISTINCT $wpdb->postmeta.post_id
FROM $wpdb->posts, $wpdb->postmeta
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->postmeta.meta_key = '_wpps_gallery_id'
AND $wpdb->posts.post_type = 'attachment'
AND $subquery1
ORDER BY $wpdb->postmeta.post_id ASC
";
$results2 = $wpdb->get_results($querystr, ARRAY_A);
//3) Search image title
$subquery1 = "$wpdb->posts.post_title LIKE '%$keyword%'"; //Takes care of the image title
$querystr = "
SELECT DISTINCT $wpdb->postmeta.post_id
FROM $wpdb->posts, $wpdb->postmeta
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->postmeta.meta_key = '_wpps_gallery_id'
AND $wpdb->posts.post_type = 'attachment'
AND $subquery1
ORDER BY $wpdb->postmeta.post_id ASC
";
$results3 = $wpdb->get_results($querystr, ARRAY_A);
//4) Search image name
$subquery1 = "$wpdb->posts.post_name LIKE '%$keyword%'"; //Takes care of the image title
$querystr = "
SELECT DISTINCT $wpdb->postmeta.post_id
FROM $wpdb->posts, $wpdb->postmeta
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->postmeta.meta_key = '_wpps_gallery_id'
AND $wpdb->posts.post_type = 'attachment'
AND $subquery1
ORDER BY $wpdb->postmeta.post_id ASC
";
$results4 = $wpdb->get_results($querystr, ARRAY_A);
//Let's merge all result arrays
$merged = array_merge($results1, $results2, $results3, $results4);
if(empty($merged)){
return false;
}
//Extract only the post id and put into array
$s_data = array();
foreach($merged as $res)
{
$s_data[] = $res['post_id'];
}
$unique_results = array_unique($s_data); //Remove duplicates
//Store result array in session variable
if(empty($unique_results)){
return null;
}else{
$searched_photos_array = WPSGalleryItem::getSearchedPhotoItemsArray($unique_results);
WPSSession::set("last_search_results", $searched_photos_array);
WPSSession::set("last_search_term", $keyword);
return $searched_photos_array;
}
}
}