Solved

changing search function to look for 2 seperate words in search

Posted on 2011-03-06
7
309 Views
Last Modified: 2012-05-11
Hi there,

I've posted similar question to this before but can't seem to get a successful answer. I'm trying to get a wordpress search to look for 2 separate words in a search. For example:

i have 3 posts with keywords:
1. fun game
2. fun
3. game

and I'm searching for the words 'fun game' BUT instead of the search results bringing up the 3 posts it's only bringing up the one post with the word fun game in it. And if I do a 2nd word such as fun play it won't bring up any results because they don't have play in it.

I hope someone can help me with this please.

here is the code that I'm trying to work with. it is a current plugin that doesn't have any more support on it.

<?php
/*
Plugin Name: Search Custom Fields
Plugin URI: http://guff.szub.net/search-custom-fields/
Description: Search post custom field values. Also provides for an alternative theme 'search' template: search-custom.php.
Author: Kaf Oseo
Version: R1.beta2
Author URI: http://szub.net

    Copyright (c) 2006, 2008 Kaf Oseo (http://szub.net)
    Search Custom Fields  is released under the GNU General Public License, version 2 (GPL2)
    http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt

    This is a WordPress plugin (http://wordpress.org).

~Changelog:
R1.beta2 (Jan-19-2008)
Bug fix to szub_search_custom_template(); a loop would occur when no
search template (search.php) existed in the active theme.
*/

function szub_search_custom_join($join) {
    global $wpdb;
    if( is_search() && szub_is_search_key() ) {
        $join = " LEFT JOIN $wpdb->postmeta ON $wpdb->posts.ID = $wpdb->postmeta.post_id ";
    }
    return $join;
}
add_filter('posts_join', 'szub_search_custom_join');

function szub_search_custom_where($where) {
    global $wp_query, $wp_version, $wpdb;
    if( !empty($wp_query->query_vars['s']) && szub_is_search_key() ) {
        $search = $wp_query->query_vars['s'];
        $key = $_GET['key'];
        $status = ($wp_version >= 2.1) ? 'post_type = \'post\' AND post_status = \'publish\'' : 'post_status = \'publish\'';
        $where = " AND $wpdb->postmeta.meta_key = '$key' AND $wpdb->postmeta.meta_value LIKE '%$search%' AND $status ";
    }
    return $where;
}
add_filter('posts_where', 'szub_search_custom_where');

function szub_search_custom_template($template) {
    if( is_search() && szub_is_search_key() && file_exists(TEMPLATEPATH . '/search-custom.php') ) {
        $template = TEMPLATEPATH . '/search-custom.php';
    }
    return $template;
}
add_filter('search_template', 'szub_search_custom_template');

function szub_is_search_key($key='') {
    if( isset($_GET['key']) ) {
        if( !empty($_GET['key']) || (!empty($key) && ($key = $_GET['key'])) )
            return true;
    }
    return false;
}
?>

Open in new window

0
Comment
Question by:carlyblack
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 35053771
Hi carlyblack,

There are a bunch of plugins that extend WordPress search to include booleans or default the list of terms to an OR separator.  

http://www.zirona.com/software/wordpress-advanced-search/

Is one...there are loads of others.  If your current plugin isn't working, switch to a new one.  
0
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 500 total points
ID: 35055094
You could alter the above like so....

UNTESTED

function szub_search_custom_where($where) {
    global $wp_query, $wp_version, $wpdb;
    if( !empty($wp_query->query_vars['s']) && szub_is_search_key() ) {
          $search = $wp_query->query_vars['s'];
          $searchWords = explode(" ", $search );
          $searchArr = array();

          foreach( $searchWords as $aWord )
               $searchArr [] = " $wpdb->postmeta.meta_value LIKE '%$aWord%' ";

          $searchTerm = implode(" or ", $searchArr );

          $key = $_GET['key'];
          $status = ($wp_version >= 2.1) ? 'post_type = \'post\' AND post_status = \'publish\'' : 'post_status = \'publish\'';
          $where = " AND $wpdb->postmeta.meta_key = '$key' AND ($searchTerm)  AND $status ";
    }
    return $where;
}

Open in new window

0
 
LVL 2

Expert Comment

by:adeelshahid
ID: 35056813
This should solve your problem.

Assuming q is the query search string



$q = trim(urldecode($_GET['q']));

if (strpos($q, ' ') !== false) {
      $search_words = explode(' ', $q);
} else {
      $search_words = array($q);
}

$where_arr = array();
$where = '';

foreach ($search_words as $word) {
      $where_arr[] = "field_name LIKE '%{$word}%'";
}

if (count($where_arr) > 0) {
      $where = implode(' OR ', $where_arr);
}

$sql = "SELECT * FROM table_name";

if (strlen($where) > 0) {
      $sql .= " WHERE $where";
}

$r = mysql_query($sql, $database);
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:carlyblack
ID: 35063155
This should solve your problem.

Assuming q is the query search string



$q = trim(urldecode($_GET['q']));

if (strpos($q, ' ') !== false) {
      $search_words = explode(' ', $q);
} else {
      $search_words = array($q);
}

$where_arr = array();
$where = '';

foreach ($search_words as $word) {
      $where_arr[] = "field_name LIKE '%{$word}%'";
}

if (count($where_arr) > 0) {
      $where = implode(' OR ', $where_arr);
}

$sql = "SELECT * FROM table_name";

if (strlen($where) > 0) {
      $sql .= " WHERE $where";
}

$r = mysql_query($sql, $database);

Hi adeelshahid,

thanks for offering this, sorry I'm only a novice, can you please let me know where i'd add this to the code?

bportlock, thanks for offering up the change, I'm not sure if I did it correctly or not, but I replaced the first bit of the plugin with your code, but I couldn't get it to search anything.

Jason1178 thanks for your offer. I had already tried that plugin but it's not doing what I needed. I basically need this plugin altered to search for the word or word and it will fulfill my needs perfectly.

thanks again for all your help, really hoping a I can get a solution.

Many thanks.
0
 
LVL 2

Expert Comment

by:adeelshahid
ID: 35063933
specifically this goes into the WHERE clause of your sql query.
0
 

Author Comment

by:carlyblack
ID: 35065893
Bportlock - sorry It was MY MISTAKE! I had replaced the wrong part of the code.

Thank you that is working very well, thanks for your help!

One final question, is there a way to adjust the order of how it is displayed in search results or does that come into the core function of wordpress?

Thanks.
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35068033
This function's purpose seems to be the provision of a WHERE clause. Whilst you could try tacking on an ORDER BY I would not advise it as you will probably have another bit of code somewhere that tacks on another ORDER BY and then you have a broken query on your hands.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question