Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am currently trying to make a business directory where the users can filter the results by:

  1. Type of service
  2. County

I am using a custom post type for the business directory, and I have created 2 custom taxonomies:

  1. Type of service - works like a category
  2. County - works like tags

I need a dynamic query that will let my users first choose a type of service, then choose a county and then get a list of results - or the other way around; they choose a county first and then a type of service.

Is this possible? Or do I have to do this with categories and sub-categories?

share|improve this question

1 Answer 1

Try this code in your template. Make sure to replace the post_type and custom taxonomies ( service & county ) with their appropriate slugs.

    $page_link = get_permalink();
    $services = get_terms( 'service' );
    $counties = get_terms( 'counties' );

    $args = array();
    $args['post_type'] = 'business';
    $args['paged'] = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    if ( isset( $_GET['service'] ) ) {
        $args['service'] = $_GET['service'];
        $page_link = add_query_arg( 'service', $_GET['service'], $page_link );
    }
    if ( isset( $_GET['county'] ) ) {
        $args['county'] = $_GET['county'];
        $page_link = add_query_arg( 'county', $_GET['county'], $page_link );
    }

    if ( is_array( $services ) ) {
        echo '<ul>';
        foreach ( $services as $service ) {
            echo '<li><a href="' . htmlentities( add_query_arg( 'service', $service->slug, $page_link ) ) . '">' . $service->name . '</a></li>';
        }
        echo '<li><a href="' . htmlentities( remove_query_arg( 'service', $page_link ) ) . '">None</a></li>';
        echo '</ul>';
    }

    if ( is_array( $counties ) ) {
        echo '<ul>';
        foreach ( $counties as $county ){
            echo '<li><a href="' . htmlentities( add_query_arg( 'county', $county->slug, $page_link ) ) . '">' . $county->name . '</a></li>';
        }
        echo '<li><a href="' . htmlentities( remove_query_arg( 'county', $page_link ) ) . '">None</a></li>';
        echo '</ul>';
    }

    query_posts( $args );
    if ( have_posts() ){
        while ( have_posts() ) {
            the_post();
            // Do Something
            // Pagination
        } 
    }
share|improve this answer
    
thanks Josua, your code with a little changes was my solution. –  Fontepink Feb 29 '12 at 16:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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