• cra

    (@cra)


    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=22 produces 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=22 gives me results in cat 21 and 22.
    Also, I’m a n00b at php. So I need lots of feedback.
    Any ideas? Thanks.

Viewing 6 replies - 1 through 6 (of 6 total)
  • grubgirl

    (@grubgirl)

    i’m eagerly awaiting anyone’s assistance with you here — do people work with post meta data stuff as a workaround or as a different approach to this? (note: despite all my buzzwords, i am not a programmer and am new to wordpress — but i am pretty darn good with RTFM and editing php files…) 😉

    Keyword search… this is precisely what I’m looking for. I’m not quite sure I understand what it is that needs to be done to implement this, however. I’m assuming the first section of code gets put on it’s own page. The second section of code gets put in the wp-blog-header file? Or somewhere else?

    Same here – I’d love to be able to set up a way for my users to search the meta data fields I’ve set up, but I’m not too experienced with PHP, so I’m curious if there is a relatively short modification that can be made to the search function (like above) but including the meta data?

    RE: cat problem.
    Instead of:
    <input type="checkbox" name="cat" value="1">
    Use:
    <input type="checkbox" name="cats[]" value="1">
    This will return all of the checked values in an array called $_REQUEST[‘cats’]. You can implode the array to get whatever delimiter you want.
    Also note that if you do a search on MySQL in a Fulltext field with the MATCH keyword, then you don’t need to do any of that sentence parsing. MySQL automatically uses +/- as you would expect on MATCH. Just a thought.

    bump

    bump

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Advanced Search’ is closed to new replies.