Jump to content

[SOLVED] Sort search results by most matching page field options


alexm
 Share

Recommended Posts

Brains working at snails pace today... Is there a simple way of sorting results from a search by the pages with the most options in a page field multi select.

e.g. you have a page field with Option 1, Option 2, Option 3 and Option 4.

One page has all 4 selected, another 2 and another 1.

I'd like to sort the search by the page with the most matching selected options first.

Link to comment
Share on other sites

You can use the count selector to get to the number of selected options, and use a descending sort order to show pages with the most selected items first:

$results = $pages->find('sort=-categories.count');

Add the rest of your selectors and replace categories with the name of your page reference field ?

See Using selectors in ProcessWire -> Count selectors.

  • Like 2
  • Thanks 1
Link to comment
Share on other sites

@MoritzLost sorry, no, this hasn't quite achieved what I want actually. I don't want to list the page that has the most checked pages in a given page field, but a page that has the most matched checked pages based on a search parameter.

e.g. if I search select (in a multi-select field) Option 1, Option 2 & Option 3 then the page with 4 selected options would show first then the next page that has the most matched checked pages.

Does that make sense?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...