Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to add some checkbox options to a sidebar search box, similar to this, where the user has the option to choose whether to search All Words, Some Word, or the Entire phrase.

I did find this after some searching - Wordpress Search Phrases. The 'sentence' option seems to work well, but the others not so good.

The code below is what I'm working on at the moment, but would really appreciate some help to get this working properly. Many thanks in advance, S. (I do not wish to use plugins for this).

<form action="<?php bloginfo('home'); ?>/" method="post">
<div class="search-icon">       
     <label for="search" accesskey="4" class="hidden">Search the site</label>                  
     <input type="text" name="s" id="search" value="Enter search term" onblur="this.value = this.value || this.defaultValue;" onfocus="this.value = '';" />             
     <input type="submit" name="submit" value="GO" class="s-btn" />
     <p><a href="#" id="search-anchor">Search Options</a></p>
     <div class="option-slide">
     <input type="radio" name="sentence" value="" checked="checked" /><label>All Words</label><br />
     <input type="radio" name="sentence" value="or" /><label>Some Word</label><br />
     <input type="radio" name="sentence" value="and" /><label>Entire phrase</label>    
     </div>   
 </div></form>    

EDIT: The answer below doesn't actually work after all : ( Anybody else have any ideas please?

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

First things first: the name attribute for your "All Words" checkbox shouldn't be 's'. That replaces the search text with "1", so when that's checked, you're searching for "1", not for the search text.

I don't think you want to use 'exact' if you're looking to replicate the example you gave in your question.

Here's an example:

Search for "Two Things" in titles and content:

Matches                   Does not match

"Two"                     "Two Things More"
"Things"                  "One or Two Things"
"Two Things"              "Two Things."

I could go on and on about what it won't match, but you get the idea. This is probably why you are getting unexpected results.

Anyway, try changing the name of the 'all words' checkbox to something that won't get matched, like 'fake_search_flag_name', since that flag is just describing WordPress' normal default search capabilities anyway.

EDIT

Ok, now that they're radios, here's something you can do:

The first one is WordPress' default behavior, so you need to make the VALUE empty:

<input type="radio" name="sentence" value="" checked="checked" /> All Words

If that box is checked, since the value is empty, WordPress will ignore it and use its default functionality. The next part is somewhat tricky. Change the values of the other two radios, something like this:

<input type="radio" name="sentence" value="or" /> Some Word<br />
<input type="radio" name="sentence" value="and" /> Entire phrase

In and of itself this will make them both behave the same way: the way you want 'Entire Phrase' to work. So we need to get 'Some Word' working. I haven't tested this next part at all, but it should work.

We need to filter the search query to allow for the search terms to match posts that have some of the words but not all of them:

add_filter( 'posts_search', 'my_awesome_posts_search_filter' );

function my_awesome_posts_search_filter( $sql, $wpq ){
  if( !isset( $wpq->query_vars['sentence'] ) || 'or' != $wpq->query_vars['sentence'] )
    return $sql;
  $new_search = str_replace( ') AND (', ') OR (', $sql );
  return $new_search;
}

That should do it.

share|improve this answer
    
Thanks for the reply - slight problem in that I've had to change the check boxes to radio boxes; so all of the input names are now 'sentence'. –  sta777 Mar 22 '11 at 15:08
    
Awesome! many thanks - is there any way that I can test it works? –  sta777 Mar 22 '11 at 16:01
1  
You could test it by trying it and seeing if it works. –  John P Bloch Mar 22 '11 at 17:02
    
Have done that; to be honest I can't see any difference. It always returns the same results regardless of which option you choose. –  sta777 Mar 23 '11 at 8:57
    
This doesn't work!!! –  sta777 Apr 19 '11 at 15:10
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.