Listify and FacetWP: How to order results by rating

David Nash wordpress 6 Comments

Listify is a WordPress directory listing theme. It allows users to submit listings (of businesses, for example) and also allows users to rate those listings. It uses the WP Job Manager plugin to manage the listings.

For some reason, it’s not currently possible to order the search results by user rating. The theme developers have told me that feature is planned for future releases.

If you purchase the FacetWP plugin, it becomes possible for the user to order the results, but rating is now included.

It took a little digging but here’s the solution, which I think is fairly elegant. Simply copy and paste this into the child theme’s functions.php:

How to get Listify and FacetWP to sort by rating: an explanation

Starting with FacetWP, I wanted to order the results by a custom field, as outlined here: https://facetwp.com/customize-the-facetwp-sort-options/ .

The problem was that WP Job Manager stores the user ratings in wp_commentmeta. So either I could write code that looks at all comments on the post, gets the ratings, and calculates the average for each result. This would probably be too slow, especially if thousands of results were found.

My other idea was that every time a rating is submitted, I could update the average in the wp_postmeta table.

It turns out that WP Job Manager Reviews add-on already has a function that does this, but it’s not automatically called – it only seems to be used when comments are removed by the admin.

So I added an action that updates the average rating for a listing and saves it as a custom field.

Now I had something I could provide to FacetWP as a custom order by option, I tried it out and it’s all working well.

And that’s how you order by user rating with Listify and FacetWP.

I shared my code with the FacetWP author and he thought it was good enough to share with others, so I thought I would too!

I’ve done quite a bit of work recently with Listify, especially having trouble geocoding thousands of business addresses. If you have any questions, let me know, and I’ll try to write more posts about it.

Comments 6

  1. This looks really promising. I’m wondering though.

    With this approach can I automatically sort by rating?

    For example http://www.geargator.net/listing-category/aerial/

    I’d like it to just list the highest rating listings first with non-rated listings being last. Do I need facetwp for this? I’m wondering if facetwp is going to be easier than just replacing the results code with my own in the file that lists the results. Since I don’t know wp shortcode this approach has helped me in other aspects of my site.

    Any advice is appreciated.

  2. Hi David,
    Awesome, I have another directory theme that I use, but also with FacetWP, I am not a developer so wondering if these steps and php code can be used exactly in other directory themes that use FacetWP? My directory theme comes with a not so good comments ratings and I would like to enable this filter at least! Thank you in advance!

  3. Hi. I have listify theme with WP Job Manager.
    I purchased FacetWP, but I cannot replicate the custom filters I had to custom facets in FacetWP.
    I don’t know how to tell FacetWP to use my WP Job manager custom filters.
    Any idea?
    thanks.

  4. Hey there!

    I’m Fernando, I work for the support department of Astoundify!

    Just wanted to let you know about our latest Listify update. We have improved the overall integration of the Facet WP plugin with our theme.

    New: Improved FacetWP integration includes:

    -Update FacetWP filter styles. Homepage facets now appear on a single line.
    -Results page now appear more visually appealing.
    -Support for new fSelect facet type.
    -Facet display can now easily be managed in “Customize > Listings > Facets”.
    -Display extra filters under a “More Filters” area.
    -Register default facets and templates for seamless activation.

    You can always ask us questions and we will be more than happy to answer them.
    Visit us here: http://themeforest.net/item/listify-wordpress-directory-theme/9602611

    Kind regards,

    Fernando

    1. Hi Maria,

      I’m Fernando, I work for the support department of Astoundify!

      You will be able to use the Facet WP plugin to add any new custom search filters on your homepage, including the Regions filter.

      Out of the box, you can use the Listing Region Search Filter as described here: http://listify.astoundify.com/article/456-filter-listings-by-region

      If you require any assistance let us know over at our help desk https://astoundify.com/support/ or you can find a lot of helpful articles over at our docs section: http://listify.astoundify.com/

      Kind regards,

      Fernando

Leave a Reply

Your email address will not be published. Required fields are marked *