Searching Custom Fields

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.

Read More

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 -->

Related posts

Leave a Reply

1 comment

  1. 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(...