Hi,
I purchased an image storefront plugin which works very well except the search function only finds exact keyword phrase matches.
I need it to find matches where all the keywords exist but not necessarily connected together precisely as they were typed.
Example:
image description content is “birds,in flight,herons,flying,gray,blue,great blue,big,large”
User types “great blue heron in flight” as their search (minus quotes).
Result should be all images that have the above content as their description.
User types “blue grouse in flight”
Result should NOT show images that have the above content as their description.
User types “heron”.
Result should be all images that have the above content as their description.
Below is the search code as it currently is designed. Can anyone tell me how to alter it to fit my needs? Thanks!
[php]<?php
class SearchImages
{
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;
}
}
}[/php]