WordPress search via metadata

Here’s what I’m trying to achieve:

I have a custom search form to search for posts based on various metadata.

Read More

This code contains the settings for the WordPress search: http://pastebin.com/5dj16Atj
The references to the custom fields are all in there.

This code is referenced in the file above and contains the actual layout for the custom search form: http://pastie.org/private/yebozm9kaqazywkilr8fkq

My main problem is that only 3 fields work in the custom search form, namely; Price Range and Area. The remaining fields such as Property Type (srch_ptype), age (srch_age) and City just don’t show up as criteria in the search. I would like all the fields to be functional and get included as criteria when people are searching.
Kindly take a look at both files to see what I have gotten wrong.

Thanks a lot.

Edit:
adomnom was kind enough to suggest that i use wp_query for the search. I would appreciate if anyone can tell me how to connect the search form with the query.

Related posts

Leave a Reply

1 comment

  1. I’m with markratledge on this one. I’d recommend looking into get_posts() or query_posts(), or even overriding the default wordpress search with special hooks.

    You should be able to do something like…


    Define the parameters of the search (more at WP_Query Page) and run it

    $searchArgs = array(
        'post_type' => 'post',
        'meta_query' = array(
            'relation' => 'AND',
            array( 'key' => 'srch_age',   'value' => $_REQUEST['srch_age'] ),
            array( 'key' => 'srch_ptype', 'value' => $_REQUEST['srch_ptype'] )
            // ... Add as many other criteria as you need
        )
    );
    
    query_posts( $searchArgs );
    while( have_posts() ): the_post();
    ...