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 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: http://i.stack.imgur.com/N8ncg.png

Thanks.

share|improve this question
    
possible duplicate of WP insert post PHP function and Custom Fields –  kaiser Sep 14 '12 at 17:06

2 Answers 2

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.

share|improve this answer

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

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

<?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(
    array(
        'key' => 'field_to_seach',
        'value' => $thesearch,
        'compare' => 'IN'
    )
)
    ));
query_posts( $args ); // alter the main query to include your custom parameters

?>
share|improve this answer
    
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 Jun 9 '12 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 Jun 9 '12 at 15:33
    
I wasn't talking about you @3ph, not to worry :). –  moraleida Jun 9 '12 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 Jun 9 '12 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 Jun 12 '12 at 22:20

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.