Assign related locations from the listing category
- Thread started
- Last updated
What you’ll achieve
Automatically select related location listings based on the category chosen for a new listing.
My site
Fill a new listing's related-location field with the locations assigned to its category. The mapping applies to new frontend submissions and leaves existing listings unchanged.
Prepare your location field
First create or retain the location listings and their listing types. This example uses these existing records:
| Listing title | Listing type | Example listing ID |
|---|---|---|
| North America | Continent | 172 |
| United States | Country | 171 |
| Canada | Country | 1086 |
| Alaska | State or Province | 47 |
| Alberta | State or Province | 50 |
The IDs are illustrative. Find your actual location IDs in Listings Manager. These are published listing records, not predefined custom-field options.
In Setup → Field Groups Manager, create a listing field group named Business locations, or reuse an existing listing group. In Setup → Custom Fields Manager, create a listing field labeled Locations, named jr_location, with type Related Listing. Select Business locations as its Field Group. In Setup → Listing Types Manager, associate that group with the listing types used by the Alaska and Alberta business categories.
In the field's Related Listings Form Filters → Listing type, include Continent, Country, and State or Province. Use your corresponding location listing types if their names differ. Leave category filters unrestricted and Logged-in user listings off so suggestions include all five records. Leave Maximum options that can be selected blank, or allow at least three selections.
Keep the field active, optional, and editable by every group that can submit these businesses. Keep its view permissions open to the visitors who should see the locations. Avoid field or field-group dependencies that remove it from the form. Include the field in applicable PaidListings plans if used. The submitter can leave Locations empty. The code fills it after form validation.
Record the category IDs and the related location listing IDs you want to assign. Keep a reference list of category IDs and location IDs when building a larger mapping. In the example, Alaska category 18 receives North America 172, United States 171, and Alaska 47. Alberta category 21 receives North America 172, Canada 1086, and Alberta 50. Use location listings that visitors can access. Replace the example IDs with those from your site.
Keep the code in your site's overrides. When creating a PHP file, start it with <?php followed by defined('MVC_FRAMEWORK') or die;. Add code to an existing PHP file without repeating those lines.
JReviews 6 Early Access
Enable Listing Form in Early Access before adding this customization.
Add the PHP code to your overrides
Joomla. Add the PHP example to templates/jreviews_overrides/hooks.php.
WordPress. Add the PHP example to jreviews_overrides/hooks.php.
Map categories to related locations
Add the mapping to hooks.php. Change jr_location in the code if your field uses another name.
fwd_add_filter('jreviews:listing.data_before_save', function (array $data, $listing): array {
if (!fwd_request()->inFrontend() || $listing->exists) {
return $data;
}
$field = $listing->getField('jr_location');
if (!$field || fwd_auth()->user()->cannot('write', $field)) {
return $data;
}
$locationsByCategory = [
18 => [172, 171, 47],
21 => [172, 1086, 50],
];
$categoryId = (int) $listing->cat_id;
if (isset($locationsByCategory[$categoryId])) {
$data['fields']['jr_location'] = $locationsByCategory[$categoryId];
}
return $data;
}, 20, 2);
The code reads the category selected on the new listing and fills its Locations field with the mapped location listings.
For a mapped category, the example replaces the entire jr_location selection. An unmapped category keeps the submitted values. An edit keeps the listing's existing values because the callback returns before changing them.
The callback also checks whether the user can write to the location field. A field with denied edit access must not receive derived values.
JReviews 5 and v6 Legacy
Add the code to your overrides
Joomla. Use templates/jreviews_overrides/filters/filter_functions.php.
WordPress. Use jreviews_overrides/filters/filter_functions.php.
Map categories to related locations
Add the following filter to the override file. It uses jr_location and three related location listings per category.
Clickfwd\Hook\Filter::add('listing_save_pre', function($data, $params)
{
if (! $params['is_new']) {
return $data;
}
else {
// Get the category
$category = $data["Listing"]["catid"];
switch ($category) {
case "18": // Alaska
$data["Field"]["Listing"]["jr_location"][0] = "172"; // North America
$data["Field"]["Listing"]["jr_location"][1] = "171"; // United States
$data["Field"]["Listing"]["jr_location"][2] = "47"; // Alaska
break;
case "21": // Alberta
$data["Field"]["Listing"]["jr_location"][0] = "172"; // North America
$data["Field"]["Listing"]["jr_location"][1] = "1086"; // Canada
$data["Field"]["Listing"]["jr_location"][2] = "50"; // Alberta
break;
//case "":
//break;
default:
return $data;
}
return $data;
}
});
Replace 18 and 21 with your category IDs. Replace each related listing ID with a location listing on your site. Change jr_location if your field has a different name. Keep the array indexes 0, 1, and 2 when assigning the three related locations.
The is_new check leaves existing listings unchanged. Categories absent from the mapping keep the submitted locations. For a mapped category, the code replaces the first three selected locations. If the form already supplies locations, check those selections before using the mapping; selections after the first three can remain.
Check the saved locations
After adding the code for your JReviews version, create a listing in each mapped category. Reopen it for editing and confirm that the selected locations match your mapping. An Alaska-category submission must show North America, United States, and Alaska, with your three corresponding listing IDs saved in jr_location. An Alberta-category submission must show North America, Canada, and Alberta. Repeat with every group that can submit listings.
Create a listing in an unmapped category and edit an existing listing. Their location values should remain unchanged. If your detail layout displays the location field, confirm that it shows the saved locations.
Learn more
- Custom Fields (v5)
- Custom Field PHP Formatting (v5)
- Custom Fields (v6)
- Custom Field Output Formatting (v6)
- Hooks (v6)