Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Robert George
PLUS
Robert George
Courses Plus Student 386 Points

Search filter

Hi I have set up parent and child categories as follows:

Destinations Africa ---botswana ---egypt ---morocco Europe ---France ---UK ---Italy

I need to set up a search filter that has two dropdown fields. First one you will select continent e.g. Africa or Europe, then the second dropdown box will auto populate with the countries belonging to continent selected in first dropdown.

I have managed to achieve this using the code below:

// Define search filter
function search_filter( $query ) {
    // only modify your custom search query.
    if ( $query->is_search &&  $_post['my_search'] == "c_search") {
        $args = array(
                'relation' => 'AND',
            array(
                'taxonomy' => 'category',
                'field' => 'id',
                'terms' => array( $_post['maincat']),
                'operator' => 'IN'
            ),
            array(
                'taxonomy' => 'category',
                'field' => 'id',
                'terms' => array( $_post['subcat']),
                'operator' => 'IN'
            )
        );
        $query->set( 'tax_query', $args);
    }
    return $query;
}

// The hook needed to search_filter
add_filter( 'the_search_query','search_filter');





<?php $media = array(
 'name'               => 'subcat',
 'hierarchical'       => 3,
 'parent'             => get_cat_id('Destinations'),
 'show_option_none'   => ('All Media'),
 'hide_empty'   => 0  ); 
?>



<form method="get" id="searchform" action="<?php bloginfo('url'); ?>/">
  <div>
    <input type="text" value="<?php the_search_query(); ?>" name="s" id="s" />
    <?php wp_dropdown_categories('name=maincat&show_option_none=All Category&id=4'); ?>
    <?php wp_dropdown_categories($media); ?>
    <input type="hidden" id="my_search" name="my_search" value="c_search" />
    <input type="submit" id="searchsubmit" value="search" />
  </div>
</form>

The problem I have now is that when you select continent and country and press search it takes you to a search results page where the url is like this /uncategorized/?level-0=6&level-1=1

How can I enable it so that when pressing the search button it takes me to the relevant url as per my parent/child categories e.g.

  • /category/destinations/africa/botswana/
  • /category/destinations/europe/france/

If you go here http://www.natgeotraveller.co.uk/destinations/ and select your destination you will see the url change to the country selected. This is what I am trying to achieve.

Please advise, I may of gone about it completely wrong.

Thanks Rob