I would like to create a custom search form to search posts based on the values of about 4 custom fields. I tried using wp_query to achieve this but so far my biggest problem is that the I don't get any results when searching for data in custom fields - even for data that I am sure exists. Also, I can't figure out how to remove the normal WordPress "keyword" search box and use just my four combo boxes.

Here is the code from my search.php file: http://pastie.org/private/jhgsmaolvjyswyhsfphlfa
and here's the code from my searchform.php: pastie.org/private/o3hrtqajh8hbkiht1nyoba
Here is an image of the form layout: https://i.sstatic.net/N8ncg.png



2 Answers 2


You need to combine your search query with a new WP Query...

That would be something like this, on your search.php


global $wp_query; // get the global object

$thesearch = get_search_query(); // get the string searched

// merge them with one or several meta_queries to meet your demand
$args = array_merge( $wp_query->query, array( 
   'meta_query' => array(
        'key' => 'field_to_seach',
        'value' => $thesearch,
        'compare' => 'IN'
query_posts( $args ); // alter the main query to include your custom parameters

  • Someone down voted this and I'd really like to know why, since this is something I use on a regular basis. If you care to explain, I appreciate it. :)
    – moraleida
    Commented Jun 9, 2012 at 13:26
  • Hi Moraleida, thanks for the answer. I'll surely try it out. I don't know who down-voted the answer, but I can't even vote because my reputation is low as a newbie here. I'll try out the code that you've suggested. My search form has 5 drop-down fields with values for those 5 custom fields. Should I just use the form as-is, with you code above?
    – 3ph
    Commented Jun 9, 2012 at 15:33
  • I wasn't talking about you @3ph, not to worry :).
    – moraleida
    Commented Jun 9, 2012 at 16:08
  • About the code usage, it depends on what really you need to achieve. Try it out with one custom field as is. If it works, then just add another 4 of the array('key' => 'field_to_seach', 'value' => $thesearch, 'compare' => 'IN') parts. Drop in if you need more help.
    – moraleida
    Commented Jun 9, 2012 at 16:12
  • Hi @moraleida, I just tried it but I'm not getting any results from custom fields in the search. Here's the code from my search.php file: pastie.org/private/jhgsmaolvjyswyhsfphlfa and here's the code from searchform.php: pastie.org/private/o3hrtqajh8hbkiht1nyoba
    – 3ph
    Commented Jun 12, 2012 at 22:20

You always can use pre_get_posts or posts_where hooks.

Here you have an example of using pre_get_posts to exclude some posts from search results: http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts#Exclude_Pages_from_Search_Results

It's better solution, because you won't make another, redundant SQL query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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