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 developing a website using WordPress and I need to make a totally customized theme. Everything seem to work well until I found something wrong at search result which doesn't match with customer requirements. I have no experience with PHP and WordPress. So let me ask two questions.

  1. How can I validate the WordPress Search Box when users do not enter any words and make search? I don't want any searching process at above case.
  2. How can I exclude Home Page from any search results?

Thanks in advance!

share|improve this question
    
What search results are you talking about? –  s_ha_dum Feb 17 '13 at 16:00
    
I mean the main search result. Let's say we want to search "Gallery". And one of the page also contains "Gallery". We then search from "Search Bar" and as a result, we will see all which contain "Gallery". Right? But I don't want that page to appear at Search Result. How can I do this? –  Thiha Maung Feb 17 '13 at 16:20
    
You tagged this as "search". There are a lot of questions in that tag about modifying the search behavior. Does none of that get you started? –  s_ha_dum Feb 17 '13 at 17:04
    
Yes. I found similar questions and tried. I couldn't find them work. Maybe someone can help me by pointing to the similar problems or resources. Thanks @s_ha_dum. –  Thiha Maung Feb 17 '13 at 17:26
    
Ok. So what did you try? –  s_ha_dum Feb 17 '13 at 17:44

1 Answer 1

up vote 0 down vote accepted

What you tried to use is broken.

add_filter( 'pre_get_posts', 'modified_pre_get_posts' ); 
function modified_pre_get_posts( WP_Query $query ) { 
  if ( $query->is_search() ) { 
    $query->set( 'post_type', array( 'page-home' ) ); 
  } 
  return $query; 
}

That function definition is not valid. You would have seen an error if you had debugging enabled. You need:

add_filter( 'pre_get_posts', 'modified_pre_get_posts' ); 
function modified_pre_get_posts( $query ) { 
  if ( $query->is_search() ) { 
    $query->set( 'post_type', array( 'page-home' ) ); 
  } 
  return $query; 
}

But that will only search for post_type = "page-home". I don't think that is correct either. I assume that is how you'd hoped to exclude the home page. In fact you did something very different. I think you want:

add_filter( 'pre_get_posts', 'modified_pre_get_posts' ); 
function modified_pre_get_posts( $query ) { 
  if ( $query->is_search() ) { 
    $query->set( 'post__not_in', array( id-of-home-page ) ); // should be a number; you have to replace that text with the actual ID
  } 
  return $query; 
}

With the default search widget, on my beaten and abused sandbox install, no search runs if I try to submit without any terms. The blog index page loads. If you want to completely interrupt the submission, though, you will need to do it with Javascript.

share|improve this answer
    
Wow! The answer's really perfect! I implemented the function like you said and I got exactly what I want. Thank you so much and really appreciate your help. I'll now try for my first question and I'll post again. –  Thiha Maung Feb 18 '13 at 6:15

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.