0

I had stored locations in wp with defined taxonomy. I would like implement a search that can take in consideration the location, eg,

search for 'car rental, Maryland'

All car rental are stored as users and every users has a location number in wp_usermeta table that map into location name. This map is done by the theme whenever a user set the field location in is account.

In my taxonomy of locations I have specified all the cities located in Maryland.

So the car rental 'XYZ' in Annapolis must result in the output of the previous search.

How can I exploit the taxonomy structure in the search?

  • I modified the question in order to be more explicit. I really don't understand neither the question nor the answer of the question as duplicate. – emanuele Oct 12 '14 at 15:04
1

Enter your form where ever you plan to display it

/**** The Form ****/
<form name="searchcars" method="get" action="http://yourdomain.com/your-page-template-slug"> 
    <label>Search</label>
    <input type="text" name="searchfor">

    <label>Location</label>
    <Select name="location">
        <?php
        $locations = get_terms('locations');
        foreach($locations as $location)
        {
        ?>
        <option value="<?php echo $location->slug; ?>"><?php echo $location->name; ?></option>
        <?php
        }
        ?>
    </select>
    <button type="select" name="search">Search</button>
</form>

Create a new file template, and assign it to a page the slug from this is used as the action for your above form.

<?php
/*
*Template Name: Custom Search
*/
$searchterm = isset($_GET['searchfor']) ? $_GET['searchfor'] : '';
$location = isset($_GET['location']) ? $_GET['location'] : ''; 

$query =  new WP_Query(array(
'post_type' => '...', /** Replace ... with the name of your custom post type **/
'location'    => $location, /** Replace the 'location' with '<name-of-your-taxonomy>' **/
'posts_per_page' => 10,
'like' => $searchterm  /** This is what we have added with the custom filter **/
)) ;

while($query->have_posts()) : $query->the_post();
 /*output the results **/
endwhile; 
?>

Enter This into your functions file to add the like paramater to wordpress queries, as an array option.

 <?php
      /** Like Filter ***/
    add_filter( 'posts_where', 'wpse18703_posts_where', 10, 2 );
    function wpse18703_posts_where( $where, &$wp_query )
    {
        global $wpdb;
        if ( $wpse18703_title = $wp_query->get( 'like' ) ) {
            $where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'' . esc_sql( like_escape( $wpse18703_title ) ) . '%\'';
        }
        return $where;
    }
    ?>

Note Reference : WP_Query with "post_title LIKE 'something%'"?

  • Please, consider the extension of the question. In which point wp guess the structure of taxonomy? It is not clear in the code. – emanuele Oct 12 '14 at 15:08

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