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 would like to delete (not allow) searches with certain keywords (eg "of", "a").

If the search is done using exactly these terms, do not return anything or redirect to 404 page ...

Thanks guys.

share|improve this question
    
I know this isn't helpful, but can I ask why you want to do that? –  Dominic Jun 23 '12 at 11:09
1  
Sure @Dominic. Some spammers tend to enter short terms as "a" or "of". On a site with many inputs and hosting "very" shared, it takes time to return results or gives error. –  BóbGCA Jun 23 '12 at 11:22
2  
possible duplicate of Set minimum number of characters in the search –  Michael Jun 23 '12 at 11:27
    
@Michael Stop words and word length are different topics. –  toscho Jun 23 '12 at 15:31
    
@toscho Sure, however the central problem is how to influence the search to deal with these words, which brings it (in my personal opinion) basically back to one topic. –  Michael Jun 23 '12 at 16:59

1 Answer 1

up vote 3 down vote accepted

Try this in your functions.php, and change news for whatever you want to be blocked in your site.

add_action('wp', 'check_search');
function check_search() {

    global $wp_query;

    if (!$s = get_search_query())
        return false;

    if (preg_match('/news/', $s)) {
        $wp_query->set_404();
        status_header(404);
        get_template_part(404);
        exit();
    }

}

Hope it helps.

share|improve this answer
    
Hi @Vinicius Massuchetto ...('/news/', $s)) unfortunately it did not work. site.com/?s=news usually is returning results. –  BóbGCA Jun 23 '12 at 13:19
    
I'm sorry. Try again with the new snippet. =D –  vmassuchetto Jun 23 '12 at 13:51
    
What a pity @Vinicius Massuchetto - Oops! Fatal error: Call to a member function set_404()... What should be happening? –  BóbGCA Jun 23 '12 at 14:17
    
I can't believe! While pasting the new snippet I forgot the global $wp_query line. With that you should have your functionality running. –  vmassuchetto Jun 23 '12 at 14:26
    
Oh, yes @Massuchetto Vinicius, global $wp_query was missing. Thank again. –  BóbGCA Jun 24 '12 at 5:02

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.