Advanced Search
-
Below is just a prototype. But so far, I seem to have successfully fund a way to search by keywords — limited to titles, excerpts, or contents. Now, what I need is a way to search by multiple categories.
So, I’m using WP as a fanfic archive, and as the archive grows, one little search field isn’t working anymore. So, I thought I’d mess with query strings. So far, I have this search.php:<html>
<body>
What are you looking for?
<form action="index.php" method="get">
Keywords (title, summary or story): <input type="text" name="s" />
Keywords (title): <input type="text" name="s_title" />
Keywords (excerpt): <input type="text" name="s_excerpt" />
Keywords (story): <input type="text" name="s_content" />
<input type="checkbox" name="cat" value="21" /> G
<input type="checkbox" name="cat" value="22" /> PG
<input type="checkbox" name="cat" value="23" /> PG-13
<input type="checkbox" name="cat" value="24" /> R
<input type="checkbox" name="cat" value="25" /> NC-17
<input type="submit" />
</form>
</body>
</html>I thought I could copy and edit the search function in wp-blog-header.php. So I have the following added after the search function:
// title search
if (!empty($s_title)) {
$s_title = addslashes_gpc($s_title);
$search = ‘ AND (‘;
// puts spaces instead of commas
$s_title = preg_replace(‘/, +/’, ”, $s_title);
$s_title = str_replace(‘,’, ‘ ‘, $s_title);
$s_title = str_replace(‘”‘, ‘ ‘, $s_title);
$s_title = trim($s_title);
if ($exact) {
$n = ”;
} else {
$n = ‘%’;
}
if (!$sentence) {
$s_title_array = explode(‘ ‘,$s_title);
$search .= ‘((post_title LIKE \”.$n.$s_title_array[0].$n.’\’))’;
for ( $i = 1; $i < count($s_title_array); $i = $i + 1) {
$search .= ‘ AND ((post_title LIKE \”.$n.$s_title_array[$i].$n.’\’))’;
}
$search .= ‘ OR (post_title LIKE \”.$n.$s_title.$n.’\’)’;
$search .= ‘)’;
} else {
$search = ‘ AND ((post_title LIKE \”.$n.$s_title.$n.’\’))’;
}
}
// excerpt search
if (!empty($s_excerpt)) {
$s_excerpt = addslashes_gpc($s_excerpt);
$search = ‘ AND (‘;
// puts spaces instead of commas
$s_excerpt = preg_replace(‘/, +/’, ”, $s_excerpt);
$s_excerpt = str_replace(‘,’, ‘ ‘, $s_excerpt);
$s_excerpt = str_replace(‘”‘, ‘ ‘, $s_excerpt);
$s_excerpt = trim($s_excerpt);
if ($exact) {
$n = ”;
} else {
$n = ‘%’;
}
if (!$sentence) {
$s_excerpt_array = explode(‘ ‘,$s_excerpt);
$search .= ‘((post_excerpt LIKE \”.$n.$s_excerpt_array[0].$n.’\’))’;
for ( $i = 1; $i < count($s_excerpt_array); $i = $i + 1) {
$search .= ‘ AND ((post_excerpt LIKE \”.$n.$s_excerpt_array[$i].$n.’\’))’;
}
$search .= ‘ OR (post_excerpt LIKE \”.$n.$s_excerpt.$n.’\’)’;
$search .= ‘)’;
} else {
$search = ‘ AND ((post_excerpt LIKE \”.$n.$s_excerpt.$n.’\’))’;
}
}
// content search
if (!empty($s_content)) {
$s_content = addslashes_gpc($s_content);
$search = ‘ AND (‘;
// puts spaces instead of commas
$s_content = preg_replace(‘/, +/’, ”, $s_content);
$s_content = str_replace(‘,’, ‘ ‘, $s_content);
$s_content = str_replace(‘”‘, ‘ ‘, $s_content);
$s_content = trim($s_content);
if ($exact) {
$n = ”;
} else {
$n = ‘%’;
}
if (!$sentence) {
$s_content_array = explode(‘ ‘,$s_content);
$search .= ‘((post_content LIKE \”.$n.$s_content_array[0].$n.’\’))’;
for ( $i = 1; $i < count($s_content_array); $i = $i + 1) {
$search .= ‘ AND ((post_content LIKE \”.$n.$s_content_array[$i].$n.’\’))’;
}
$search .= ‘ OR (post_content LIKE \”.$n.$s_content.$n.’\’)’;
$search .= ‘)’;
} else {
$search = ‘ AND ((post_content LIKE \”.$n.$s_content.$n.’\’))’;
}
}I’m trying to figure out how to search by multiple categories. Right now, I figure, with the uri query string, I’d need to create something like
?cat=21+22+23. Because?cat=21&cat=22produces a search that nulls the first cat (21) and only lists results in the second one (22).
So, I’d like to do either of the following two:
1) create a form that gives me a uri such as?cat=21+22+23
or
2) do something so that?cat=21&cat=22gives me results in cat 21 and 22.
Also, I’m a n00b at php. So I need lots of feedback.
Any ideas? Thanks.
The topic ‘Advanced Search’ is closed to new replies.