Hook: mam_gd_listings_sort_by_distance

Signature

apply_filters( 'mam_gd_listings_sort_by_distance', bool $sort_by_distance, array|null $user_location );
Parameter Type Description
$sort_by_distance bool true when the Display order using Distance from User app setting (tsl-setting-directory_display_distance_from_user) is yes for the current user’s role, otherwise false.
$user_location array|null The user location read from the request as array( lat, lon ) floats (actual_lat / actual_lon first, then lat / lon), or null when the request has no usable location.

Returns: bool. true sorts nearest-first, false sorts by title. The return value is cast to bool.


Purpose

Lets code choose the default order of a GeoDirectory listings response without changing the app setting. For example, sort nearest-first on one site even though the setting is off, or turn distance sorting off for a particular role.

The filter only decides the order. It does not add or remove the distance values on each row; those are added whenever the request carries a usable user location (see Mobile listing data shape).


When it runs

Once per listings response, in mam_geodirectory_content::get_data_for_app() (content-classes/local-app-geodirectory-v2-class.php), after every row has been built and after distance has been added to each row that has coordinates. It runs before Hook: mam_geodirectory_final_listings.

It does not affect the events-only (calendar) content type, which is always ordered by sort_date.


Default behavior

  • The setting value is passed in unchanged.
  • If the filter returns true but no row has a numeric distance (for example, the app sent no location), the list falls back to title order.
  • In a distance sort, rows without a distance follow the rows that have one, ordered by title. Rows at the same distance are ordered by title.

Examples

Always sort nearest-first when the app sends a location

add_filter( 'mam_gd_listings_sort_by_distance', function ( $sort_by_distance, $user_location ) {
    return null !== $user_location ? true : $sort_by_distance;
}, 10, 2 );

Never sort by distance

add_filter( 'mam_gd_listings_sort_by_distance', '__return_false' );

Rows still carry distance, so on iOS 26.40.14+ users can still pick Distance (Nearest) from the sort sheet.


Gotchas

  • Returning true is not enough on its own. The list is sorted by distance only when at least one row has a numeric distance.
  • $user_location is null without a usable location. Out-of-range coordinates and 0, 0 are treated as no location.
  • The setting is read per role. The incoming value comes from the App Settings cascade for the current user’s role, not from a plain WordPress option.

Verification

This article was last verified against:

  • Plugin: mam-geodirectory 26.40.3
  • Source: content-classes/local-app-geodirectory-v2-class.php (get_data_for_app(), resolve_request_user_location())

Re-verify whenever the sort block in get_data_for_app() changes, the filter’s arguments change, or the way the user location is read from the request changes.


  • Plugin: mam-geodirectory
  • Content type: GeoDirectory list and map
  • Mobile listing data shape
  • Hook: mam_geodirectory_final_listings

Metadata

Field Value
Article type Hook Reference
Plugin slug mam-geodirectory
Applies to plugin version 26.40.3+
Category Extending MAM Suite
Hook type filter
Audience PHP developer
Last verified 2026-10-01
Was this article helpful?
Contents

    Need Support?

    Can't find the answer you're looking for? Don't worry we're here to help!