Jump to content
Torsten Baldes

$pages->find() get by id and maintain order

Recommended Posts

Hi,

I have multiple $pages->find() operations which result in multiple page ids.

e.g.: 

$find1: 8044|10045|3702|11067
$find2: 2004|5421|9392

Next i'm searching these ids to get a new WireArray, which i can limit and paginate:

$unifiedMatches = $pages->find("id=$find1|$find2, limit=$limit");

This works all as intended. The problem is, that this new collection of pages doesn't maintain the order of the ids in the selector. They are sorted by their id.

$unifiedMatches: 2004|3702|5421|8044|9392|10045|11067

Is there a way to get the pages in the $unifiedMatches result in the order of the selector input?

$unifiedMatches: 8044|10045|3702|11067|2004|5421|9392

Thanks!

Share this post


Link to post
Share on other sites

Hi Torsten

thanks for the question, I also stumbled upon this problem and couldn't find a solution so far. There are simple solutions in ExpressionEngine and Craft (https://docs.expressionengine.com/latest/channels/entries.html#fixed_order, https://docs.craftcms.com/v3/dev/element-queries/entry-queries.html#fixedorder), maybe I missed something.

Here's a workaround, temporary test code from a current project:

$itemIdsA ia an array of ids, $finder3A is an array with results from a rockfinder query.

if(count($itemIdsA)) {
	foreach($itemIdsA as $key => $value) {
		foreach($finder3A as $item) {
			if($value == $item['id']) {
				echo '<li>';
				$datumVon = strtotime($item['datum_von']);
				echo 'Autor: ' . $item['autoren_title'] . ' / Datum: ' . date('d.m.Y H:i', $datumVon) . '<br />';
				echo '<h3>' . $item['title'] . '</h3>';
				echo '<p>' . $item['teaser'] . '</p>';
				if(isset($pma[$item['id']])){
					echo '<p>Test Anzahl Leserbriefe: ' . $pma[$item['id']][0] . '</p>';
				}
				echo '</li>';
			}
		}
	}
}

 

  • Like 1

Share this post


Link to post
Share on other sites

Thanks,

but I'm afraid this doesn't work with pagination.

At first I created a new wirearray and added the others. This worked regarding maintaining the order, but got me problems with the pagination. Your approach would possibly work too, but I would have the same pagination trouble. 😕

Share this post


Link to post
Share on other sites
2 hours ago, Torsten Baldes said:

$unifiedMatches = $pages->find("id=$find1|$find2, limit=$limit");

 

Did you try someting like

$unifiedMatches = $pages->find("id=$find1, limit=$limit")->add($pages->find("id=$find2, limit=$limit"));

? Seems to work with a quick test here...

Share this post


Link to post
Share on other sites
Posted (edited)

My workaround was because of unsorted query results from a Page Reference field inside a Repeater Matrix field. If you can use $pages->find(), the results should get sorted: Try loadOptions and set joinSortfield to true. Test:

$items = $pages->getById([1182,1061,1079,1082]);
d($items);

The result is a PageArray, the items => array is sorted in the order of the ids in your selector.

Update: It doesn't seem to work with $pages->find(), but you can use $pages->getById.

Edited by Lutz
update

Share this post


Link to post
Share on other sites

@dragan @bernhard

Yes adding works to get a "unified" PageArray with the right order, but the pagination for this remains empty.

$unifiedMatches = $find1->add($find2)->find("limit=$limit");

$unifiedMatches->renderPager() returns nothing.

Share this post


Link to post
Share on other sites

Ah, sorry, didn't see that 😐 

So what are your finds sorted by? Could you share those find queries with us?

Share this post


Link to post
Share on other sites
11 minutes ago, bernhard said:

So what are your finds sorted by? Could you share those find queries with us?

The sorting of these queries is not really important. The important thing for me is, that the results from $find1 are in front of $find2.

It's like a lazy weighted search, where I first search the titles of the pages for a keyword/phrase ($find1) and then search the other fields (body etc.) for the same keyword (excluding the ids from $find1). The logic here is, that pages, which have the keyword in their title, are more relevant/important than the other results and should come first.

Share this post


Link to post
Share on other sites
12 minutes ago, Torsten Baldes said:

It's like a lazy weighted search, where I first search the titles of the pages for a keyword/phrase ($find1) and then search the other fields (body etc.) for the same keyword (excluding the ids from $find1). The logic here is, that pages, which have the keyword in their title, are more relevant/important than the other results and should come first.

Thx for clarifying, that makes things clear but complicated 😄 

I don't think that there is a way to achieve this on the db side. Only chance is on the PHP side. Does something like this work?

 

Share this post


Link to post
Share on other sites

There is some code and some links to explore in this post:

 

  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...