Jump to content

search code problems


shiftaction

Recommended Posts

Hello!  I am new to this forum and to coding as well.  I am trying to resolve some issues with this piece of code that I believe runs how my search works.  I want the search to search terms in the title and contents and retrieve the results.  The problem I am having is that if there is an empty space in front of something it does not search right.  As in "dell laptop".  If you search for "laptop dell" you get no results.  And if you search only " dell" with the empty space in front of it, you get no results if 'dell' is at the beginning of the title.  I want the search to search all words and mix the results, but also I was having the problem that if i searched for "dell laptop" I would get results for dell and laptop, bringing up gateway/sony and others because it was streaming from the 2 results instead of them both as 1 result together (for each result it brings back to the screen of course).  Here are different strands of code used.  Newest on bottom.....any help?  I am lost!

 

 

 

 

//////////////////////////////////////////////////////////

 

function Theme_posts_where( $where ) {

 

global $wpdb, $term;

$where .= " AND ({$wpdb->posts}.post_title LIKE '%$term%' OR {$wpdb->posts}.post_content LIKE '%$term%')";

 

return $where;

}

 

 

//////////////////////////////////////////////////////////

 

function Theme_posts_where( $where ) {

 

global $wpdb, $term;

$term1 = explode(" ",$term);

$xl = '';

 

foreach($term1 as $tt)

{

$xl .= " OR ({$wpdb->posts}.post_title LIKE '%$tt%' OR {$wpdb->posts}.post_content LIKE '%$tt%')";

 

}

 

$where .= " AND (({$wpdb->posts}.post_title LIKE '%$term%' OR {$wpdb->posts}.post_content LIKE '%$term%') $xl )";

 

return $where;

}

 

Link to comment
https://forums.phpfreaks.com/topic/284341-search-code-problems/
Share on other sites


mysql> SELECT * FROM shiftaction;
+----+-------------+
| id | title |
+----+-------------+
| 1 | Dell tablet |
| 2 | Dell laptop |
| 3 | Sony laptop |
| 4 | Dell pc |
+----+-------------+
4 rows in set (0.00 sec)

mysql> SELECT * FROM shiftaction
-> WHERE title LIKE '%dell%' AND title LIKE '%laptop%';
+----+-------------+
| id | title |
+----+-------------+
| 2 | Dell laptop |
+----+-------------+
1 row in set (0.00 sec)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.