> For the complete documentation index, see [llms.txt](https://whoisandywhite.gitbook.io/porterwp/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://whoisandywhite.gitbook.io/porterwp/developer-api/hooks/parse_geo_filter.md). Agents can also search and read this documentation through its [MCP server](https://whoisandywhite.gitbook.io/porterwp/~gitbook/mcp).

# parse\_geo\_filter

**Type:** Filter

**Location:** `includes/premium/filters/traits/trait-FilterParser.php`

```php
apply_filters(
    'parse_geo_filter',
    [],
    $query['post_type'] ?? 'post',
    $lat_lng,
    $distance,
    'miles'
);
```

## Description

Converts a validated postcode origin and distance into theme-specific `WP_Query` amendments.

PorterWP validates and geocodes the postcode, but it does not know how a theme stores locations. A theme that enables the Geo filter must register this hook and return query arguments such as `post__in` or a suitable `meta_query`. Porter merges the returned array over the query being filtered.

## Parameters

| Position | Variable      | Description                                                                   |
| -------- | ------------- | ----------------------------------------------------------------------------- |
| 1        | `$amendments` | Empty amendment array supplied by Porter. Return an array of query arguments. |
| 2        | `$post_type`  | Current query post type; may be a slug or an array of slugs.                  |
| 3        | `$lat_lng`    | Validated origin as `['lat' => float, 'lng' => float]`.                       |
| 4        | `$distance`   | Validated maximum distance.                                                   |
| 5        | `$unit`       | Currently always `miles`.                                                     |

## Example

This example assumes an ACF Google Map field called `location`. It deliberately caps the candidate query before calculating distances in PHP.

```php
add_filter( 'parse_geo_filter', function ( $amendments, $post_type, $origin, $distance, $unit ) {
    if ( 'office' !== $post_type || 'miles' !== $unit ) {
        return $amendments;
    }

    $candidate_ids = get_posts( [
        'post_type'      => 'office',
        'post_status'    => 'publish',
        'posts_per_page' => 500,
        'fields'         => 'ids',
        'no_found_rows'  => true,
    ] );

    $matching_ids = [];

    foreach ( $candidate_ids as $post_id ) {
        $location = get_field( 'location', $post_id );

        if (
            is_array( $location ) &&
            isset( $location['lat'], $location['lng'] ) &&
            PorterFiltersBase()->is_within_distance(
                $origin['lat'],
                $origin['lng'],
                $location['lat'],
                $location['lng'],
                $distance
            )
        ) {
            $matching_ids[] = (int) $post_id;
        }
    }

    $amendments['post__in'] = $matching_ids ?: [ 0 ];
    return $amendments;
}, 10, 5 );
```

For a large location dataset, calculate proximity in an indexed spatial store or a bounded database query rather than loading every post.

## Validation and units

The normal signed Geo control uses distance options from `porter/filter/geo_distance_options`. Defaults are 5, 10, 20, 30, and 50 miles. Porter keeps at most ten unique integer choices between 1 and 100.

```php
add_filter( 'porter/filter/geo_distance_options', function ( $distances ) {
    return [ 10, 25, 50 ];
} );
```

The callback runs only for a signed form, a server-registered legacy form, or the narrowly validated legacy geo path described in [Filter request security and compatibility](https://whoisandywhite.gitbook.io/porterwp/features/premium-filters-block/premium-filter-security.md).

## Postcode lookup safeguards

Porter currently supports UK postcodes and resolves them through Postcodes.io. The lookup:

* strictly validates and normalizes the postcode before making a request;
* uses `wp_safe_remote_get()` with a three-second timeout;
* permits one redirect and limits the response to 64 KiB;
* caches successful coordinates for seven days;
* caches failures for 15 minutes; and
* limits uncached outbound lookups per remote-address hash to 20 in five minutes by default.

The rate limit can be adjusted between 1 and 100 lookups:

```php
add_filter( 'porter/filter/geo_rate_limit', function ( $limit ) {
    return 10;
} );
```

## Compatibility

Older callbacks registered with four accepted arguments continue to work; WordPress simply does not pass the fifth unit argument to them. New code should register five arguments and check the unit explicitly.
