0

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?

2

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
        } 
    }
  • thanks Josua, your code with a little changes was my solution. – Fontepink Feb 29 '12 at 16:20

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.