Implementing specific search behavior via category attribute

Hello HivePress team,

I need your help implementing a specific search behavior on my HivePress-based site.

Context:

  • I have a custom Listing attribute called “service_areas” (slug: service_areas).
  • It’s a multi-select location attribute that includes individual city options (e.g. City1, City2) and one special option called “Whole Country”. (so if they service all the cities, they do not need to select all the cities (100+))
  • Vendors can select one or more cities, or they can select “Whole Country” to indicate they work nationwide.

Current behavior:
Client first choose the listing category they want, then choose the location (slug: “service_areas”) and click search

  • When a client filters by a specific city (e.g. City1), only listings explicitly tagged with City1 appear.
  • Listings that have only the “Whole Country” option do not appear, even though those vendors work everywhere.

Desired behavior:

  • If a user filters by any city (for example “City1”), the results should include:
    :white_check_mark: Listings tagged with “City1”
    :white_check_mark: Listings tagged with “Whole Country”
  • In other words, “Whole Country” should act as a wildcard option that always matches any city filter.

Example:

  • Vendor A: service_areas = [City1]
  • Vendor B: service_areas = (Whole Country)
  • User searches for City1→ both Vendor A and Vendor B should appear.

My question:
What is the recommended way in HivePress to achieve this filtering behavior?

Is there a supported approach or hook to modify the attribute search query so that the “Whole Country” option always matches any selected city?

Alternatively, is there an example of how to implement this kind of “wildcard” value in a custom attribute filter in a way compatible with HivePress’s internal query structure?

Hi,

Implementing this search logic would require code customizations for the hivepress/v1/models/listing/search hook, using this hook you can alter the search query. The function parameter is the WP_Query object WP_Query – Class | Developer.WordPress.org

I recommend using the “none selected” drop-down state as the “whole country” option, for example you can add this to the field description (e.g. “select cities where you operate or none if you operate countrywide…”). In this case search will work as expected, and the issue would be more simple – to customize how the attribute is displayed so it would display “Whole Country” for listings if no options are selected.

Hope this helps

1 Like

Thank you, ihor!

I managed to achieve the wanted result by including the listings with location tag “Whole Country” into every city search.
This is the my snippet in case anyone need it something like this:

add_filter( 'hivepress/v1/models/listing/search', function ( $query ) {
    if ( is_admin() ) return $query;

    $tax_query = $query->get( 'tax_query' );
    if ( ! is_array( $tax_query ) ) return $query;

    $new_tax_query = [];
    $service_area_found = false;

    foreach ( $tax_query as $clause ) {
        if ( isset( $clause['taxonomy'] ) && $clause['taxonomy'] === 'hp_listing_service_areas' ) {
            $service_area_found = true;

            $new_tax_query[] = [
                'relation' => 'OR',
                $clause,
                [
                    'taxonomy' => 'hp_listing_service_areas',
                    'field'    => 'slug',
                    'terms'    => [ 'whole-country' ],
                    'operator' => 'IN',
                ]
            ];
        } else {
            // Keep all other filters (like category) untouched
            $new_tax_query[] = $clause;
        }
    }

    if ( $service_area_found ) {
        $query->set( 'tax_query', $new_tax_query );
    }

    return $query;
});
1 Like

Thank you for your solution. It will be useful for our community.

1 Like

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