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 know how I can set up a form (separate from the normal search form), that will allow me to search through a particular custom post type.

Within my defined custom post type I have 2 fields. In the search the user should be able to fill in any of them, in order to narrow down his search.

Any examples of code that could accomplish this?

This is what I've tried to do till now. The house name is stored as the custom post's title, while city and state are two custom fields. This search form is not working as it is.

    <div id="primary">
        <div id="content" role="main">

        <?php

        global $wp_query;

        $housename=mysql_real_escape_string($_GET['housename']);

        $city=mysql_real_escape_string($_GET['city']);

        $state=mysql_real_escape_string($_GET['state']);        

        $args = array(
                'post_type' => 'house',
                'post_title' => $housename,
                'meta_query' => array(
                        array(
                            'key' => 'house_city',                  
                            'value' => $city                            
                        ),
                        array(
                            'key' => 'house_state',                  
                            'value' => $state                           
                        )  
                )                      
        );

        $houses_found = new WP_Query( $args );  

        if ( $houses_found->have_posts() ) :
            while ( $houses_found->have_posts() ) : $houses_found->the_post();
                echo '<p><a href="'.get_permalink().'">'.get_the_title().'</a></p>';

            endwhile;
        endif;

        wp_reset_postdata();

        ?>

        <form method="get" id="searchform" >
            <label for="housename"><?php _e( 'Name' ); ?></label>
            <input type="text" class="field" name="housename" id="housename" />
            <label for="city" ><?php _e( 'City' ); ?></label>
            <input type="text" class="field" name="city" id="city" />
            <label for="state" ><?php _e( 'State' ); ?></label>
            <input type="text" class="field" name="state" id="state" />             
            <input type="submit" class="submit-2" name="submit" id="searchsubmit-2" value="<?php esc_attr_e( 'Search' ); ?>" />
        </form>


            <?php while ( have_posts() ) : the_post(); ?>

                <?php get_template_part( 'content', 'page' ); ?>

            <?php endwhile; // end of the loop. ?>

        </div><!-- #content -->
    </div><!-- #primary -->
share|improve this question
    
Does the query itself work without using dynamic values(ie make $housename, $city, and $state values static)? If yes, then can you echo out the 3 search variables after $_GETting them? –  Ben Sep 10 '12 at 16:55
    
I can echo the 3 search variables without problems, but the query is still problematic. If I have any blank fields it doesn't work. On the other hand, I tried disabling the post_title parameter, and the second house_state array meta query, leaving just the house_city, and it worked when I input a city, returning a few results correctly. So for sure there is an issue with blank input fields. I'd like users to be able to input as much info as they know, then have the system use that info accordingly in its query. –  drtanz Sep 10 '12 at 17:43
add comment

1 Answer

up vote 0 down vote accepted

Updated this answer in response to first comment below:

if(!empty($_GET['city']) AND !empty($_GET['state']))
{ // if both fields are filled out, find houses that match both
$relation = "AND";
}
elseif(empty($_GET['city']) OR empty($_GET['state']))
{ // if either of the fields are empty, find houses that match either field
$relation = "OR";
}

$args = array(
     'post_type' => 'house',
     'post_title' => $housename,
     'meta_query' => array(
     'relation' => $relation,// use $relation variable instead of string here
     array(
    'key' => 'house_city',                  
    'value' => $city                            
     ),
     array(
    'key' => 'house_state',                  
    'value' => $state                           
     )  
     )                      
);

I'm not sure but I think having the $housename empty would look for houses with an empty post title. You may need to conditionally add that part to your query, something like this could work:

if(!empty($_GET['housename']))
{
    $houseNameQuery = "'post_title' => $_GET['housename']";
}

Then in your $args replace the 'post_title' parameter:

$args = array(
        'post_type' => 'house',
        $houseNameQuery,
        'meta_query' => array(...
share|improve this answer
    
Not exactly. I want them to enter either state or city, or else both city and state for further narrowing down. Many times they wouldn't know the house name either, which is being queried as the post title. I don't know if leaving that blank will throw off the query. –  drtanz Sep 10 '12 at 21:13
    
Well I'm not sure what I'm thinking is the best way: but, you could change the 'relation' value, depending on what fields were filled out. See my updated answer above. –  Ben Sep 10 '12 at 22:10
    
Thanks, I'm opening a new question as the scope of this one is fulfilled, so its solved. –  drtanz Sep 11 '12 at 8:51
add comment

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.