1

The site I'm building has a directory element, which has a lot of different business listings; this is a custom post type. It has a Custom Taxonomy called Trades under which are different types of businesses, e.g plumbers, electricians, decorators, accountants etc. I've written a search form and put it into searchform.php, which is then called by get_search_form(), here it is:

<form role="search" class="main-search" action="<?php bloginfo('home') ?>" method="get">
    <div class="search-container">
        <input type="text" class="main-search-field" placeholder="Search for a service..."  name="s" id="s">
    </div>
    <input type="hidden" name="post_type" value="directory">
    <div class="submit-container">
        <button type="submit" class="main-submit-btn">Search</button>
    </div>
</form>

As you can see, I have narrowed the search down to my Custom Post Type ('directory') but I'm not sure how to narrow it down further. Essentially, when text is entered into the search box, and the user presses enter, I want it to search all my Trade terms, instead of searching post titles/content. Does anyone know how I'd go about doing this?

Thanks for any help :)

1

For now I've just redirected the default search string (www.sitedomain.com/?s=keyword) to www.sitedomain.com/trade/keyword, (trade is my custom taxonomy) although I'm pretty sure this isn't the correct or best way to do it so it'd be great if someone else has a better solution. Here is the code for redirecting the search string:

function redirect_search() {
    if ( is_search() && ! empty( $_GET['s'] ) ) {
        wp_redirect( home_url( "/trade/" ) . urlencode( get_query_var( 's' ) ) );
        exit();
    }
} add_action( 'template_redirect', 'redirect_search' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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