shiftaction Posted November 27, 2013 Share Posted November 27, 2013 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; } Quote Link to comment Share on other sites More sharing options...
Barand Posted November 27, 2013 Share Posted November 27, 2013 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) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.