JReviews logo
Guides My site
← All guides Community guide

Show featured listings first, then title

C Alejandro
Thread started by CMoore. Contributors Alejandro.
Thread started
Last updated

What you’ll achieve

Shuffle featured listings to the top of a category list, then show the rest by title from Z to A.

My site

Platform
JReviews version

Put featured restaurants first in an order that can change on refresh, then list the rest by title from Z to A.

The example category is Restaurants. Featured listings are Harbor Grill and Cedar Cafe. The others are Maple Diner and River Loft. After the customization, a category list using the new sort option shows the two featured listings first. Refreshing the page can swap those two. River Loft then Maple Diner follow, because title order is descending.

Do not choose Featured Random on a Listings module. That setting shows only featured listings, shuffled, and leaves the rest of the category out.

Do not choose Featured Random on a Listings widget. That setting shows only featured listings, shuffled, and leaves the rest of the category out.

Built-in Featured on a list page puts featured listings first, then the rest by newest. It does not shuffle the featured group, and it does not sort the rest by title. Use the steps below for that mixed order.

Shuffling the featured group can slow a large directory.

Keep the customization in your JReviews overrides. Start a new PHP file with <?php followed by defined('MVC_FRAMEWORK') or die;. In an existing file, add the callback without repeating the opening tag or guard. If a snippet includes those lines, keep each only once.

  1. In Setup → Listing Types Manager, create a listing type named Restaurant, or open the type you already use for restaurants.
  2. In Setup → Categories Manager, create Restaurants and assign that listing type, or open the category you already use.
  3. Create four published listings in Restaurants titled Harbor Grill, Cedar Cafe, Maple Diner, and River Loft.
  4. Open Harbor Grill and Cedar Cafe. Turn Featured on for both. Leave Maple Diner and River Loft unfeatured. Turn Featured off on every other listing in Restaurants, so the two featured titles are the only featured listings in that category.
  5. In JReviews → Configuration, open List Page Configuration. In Ordering options, turn on Show ordering dropdown list. If the Restaurant listing type has its own Ordering Options on the Configuration tab, turn the same setting on there too.

The new label appears in the list page dropdown after you add the code. It does not appear in those checkboxes.

It does not appear on a Listings module.

It does not appear on a Listings widget.

Joomla. Create a JReviews → List Pages menu item. Set Type of Menu to Category List, select Restaurants, and publish it.

WordPress. Open Pages, create a page titled Restaurants, and in JReviews Menu System select List Pages. Choose Custom List, set Category to Restaurants, and publish the page.

Open the JReviews dashboard, select Manage Early Access, and turn on Listing List Pages.

Joomla. Use templates/jreviews_overrides/hooks.php.

WordPress. Use jreviews_overrides/hooks.php.

They add the dropdown label Featured random, then title.

If hooks.php does not exist, save this complete file.

<?php
defined('MVC_FRAMEWORK') or die;

fwd_add_filter('jreviews:listings_list.order_options', function (array $options): array {
    $options['featured_then_title'] = 'Featured random, then title';

    return $options;
});

fwd_add_filter('jreviews:listings_list.query', function ($query) {
    $query->getModel()->addSortingOption('featured_then_title', function ($builder) {
        $model = $builder->getModel();

        $builder->joinCustomFields();
        $builder->orderBy('featured_listing', 'desc');
        $builder->orderByRaw('CASE WHEN featured_listing = 1 THEN RAND() ELSE 0 END');
        $builder->orderBy($model->qualifyColumn($model::COL_TITLE), 'desc');
    });

    return $query;
});

If hooks.php already exists, add the marked lines at the end of the file. The copy button copies only those lines.

<?php
defined('MVC_FRAMEWORK') or die;

fwd_add_filter('jreviews:listings_list.order_options', function (array $options): array {
    $options['featured_then_title'] = 'Featured random, then title';

    return $options;
});

fwd_add_filter('jreviews:listings_list.query', function ($query) {
    $query->getModel()->addSortingOption('featured_then_title', function ($builder) {
        $model = $builder->getModel();

        $builder->joinCustomFields();
        $builder->orderBy('featured_listing', 'desc');
        $builder->orderByRaw('CASE WHEN featured_listing = 1 THEN RAND() ELSE 0 END');
        $builder->orderBy($model->qualifyColumn($model::COL_TITLE), 'desc');
    });

    return $query;
});

Joomla. Use templates/jreviews_overrides/filters/filter_functions.php.

WordPress. Use jreviews_overrides/filters/filter_functions.php.

They add the dropdown label Featured random, then title.

If filter_functions.php does not exist, save this complete file.

<?php
defined('MVC_FRAMEWORK') or die;

Clickfwd\Hook\Filter::add('listing_list_ordering_select', function ($orderingParams, $params) {
    $orderingParams['options']['featured_then_title'] = 'Featured random, then title';

    return $orderingParams;
});

Clickfwd\Hook\Filter::add('pre_get_listings_listpage_query', function ($repository, $params) {
    if (($params['sort'] ?? '') !== 'featured_then_title') {
        return $repository;
    }

    $order = [
        'Field.featured DESC',
        'CASE WHEN Field.featured = 1 THEN RAND() ELSE 0 END',
        'Listing.' . EverywhereComContentModel::_LISTING_TITLE . ' DESC',
    ];

    $repository->order($order, true);
    $repository->getModel()->order = $order;

    return $repository;
});

If filter_functions.php already exists, add the marked lines at the end of the file. The copy button copies only those lines.

<?php
defined('MVC_FRAMEWORK') or die;

Clickfwd\Hook\Filter::add('listing_list_ordering_select', function ($orderingParams, $params) {
    $orderingParams['options']['featured_then_title'] = 'Featured random, then title';

    return $orderingParams;
});

Clickfwd\Hook\Filter::add('pre_get_listings_listpage_query', function ($repository, $params) {
    if (($params['sort'] ?? '') !== 'featured_then_title') {
        return $repository;
    }

    $order = [
        'Field.featured DESC',
        'CASE WHEN Field.featured = 1 THEN RAND() ELSE 0 END',
        'Listing.' . EverywhereComContentModel::_LISTING_TITLE . ' DESC',
    ];

    $repository->order($order, true);
    $repository->getModel()->order = $order;

    return $repository;
});

In the JReviews control panel, run Clear Cache & File Registry.

  1. Open the published JReviews → List Pages menu item for Restaurants.

  2. In the sort dropdown, choose Featured random, then title.

  3. Confirm Harbor Grill and Cedar Cafe show first. Refresh once. Those two can swap.

  4. Confirm River Loft is next and Maple Diner is last. Other restaurants already in the category still appear in that same order: featured titles first, then the rest by title from Z to A.

  5. Open the published Restaurants page.

  6. In the sort dropdown, choose Featured random, then title.

  7. Confirm Harbor Grill and Cedar Cafe show first. Refresh once. Those two can swap.

  8. Confirm River Loft is next and Maple Diner is last. Other restaurants already in the category still appear in that same order: featured titles first, then the rest by title from Z to A.

Default Ordering in List Page Configuration does not offer this value. Choose it from the list dropdown.

On a Custom List, leave Custom Query (JSON) without an order key. After the dropdown shows the correct order, copy order=featured_then_title into Custom Parameters so the page opens on this order.

On a Custom List, copy order=featured_then_title into Custom Parameters so the page opens on this order.

A refresh can change the featured order. Page 2 can show a featured listing that also appeared on page 1.

Learn more