Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Category search filter

Featured Replies

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

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.