Jump to content

Sort with has_parent ?


cst989
 Share

Recommended Posts

I would like to sort search results so that those with a specific root parent ID (could be the immediate parent, could be one higher) are prioritised, essentially pinned to the top of the results. How can I do this please? My existing query is very straightforward (using SearchEngine module for index):

$pages->find('search_index%=' . $query . ', limit=15');

Essentially what I want is something like this:

$pages->find('search_index%=' . $query . ', sort=[has_parent=1234], limit=15');

Thanks.

Link to comment
Share on other sites

One method would be to combining two finds, something like this:

$results = $pages->find("search_index%=$query, has_parent=1234");
$moreResults = $pages->find("search_index%=$query, has_parent!=1234");
$allResults->import($moreResults);
$limitedResults = $allResults->slice(0, 15);

Note that setDuplicateChecking() can be useful with import() when doing this sort of thing.

Perhaps someone else knows how to do this with a single selector!

  • Thanks 1
Link to comment
Share on other sites

Ah yes, that's something I thought of and an elegant solution.

But yeah a single selector I think is something I need, as unfortunately there's one additional thing I should've mentioned in the original post, I need to render pagination! And I don't think that will play nicely with combining queries and slices.

Link to comment
Share on other sites

I didn't think of pagination! You could make the last line a find() and, though I haven't tried this, you might be OK.

$limitedResults = $allResults->find('limit=15');
echo $limitedResults->render();

 

  • Thanks 1
Link to comment
Share on other sites

Thank you, that worked!

I wanted to loop through the results to template them manually and for some reason I had to change the following, which did nothing:

echo $limitedResults->renderPager();

to

$pager = $modules->get("MarkupPagerNav");
echo $pager->render($limitedResults);

but otherwise, got there in the end.

  • Like 1
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...