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

Where are the "level"s coming from?
Looks to be something returning a wrong array value.

Well there’s 2 ways to do it;

1: Javascript catches the changing form elements and updates the form’s action respectively.
2: Push the form data to a PHP handler, which sends a [FPHP]header[/FPHP] element with the Location set to the target URL.

(I guess “3. Have apache rewrite the URL” is also an option, but not one i’d consider personally.)

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.