Jump to content
Tyssen

Get random items from repeater field from specific pages

Recommended Posts

I have a series of pages that use a repeater field and I want to fetch 3 random rows from each repeater from 5 specific pages, so that I'll have a total of 15 items output, but then also randomise the output so that items from the same repeater field aren't next to each other.

Originally I had 

$selected_pages = $pages->getById([1049,1053,1055,1059,1152]);
		foreach($selected_pages as $selected_page) :
			foreach($selected_page->repeater_field->getRandom(3) as $item) :
				Output
		endforeach;
endforeach;

That gave me the pages randomised, but also gave me the items from each page's repeater next to each other.

So then I tried

$selected_pages = $pages->get("id=1049|1053|1055|1059|1152")->repeater_field->find("limit=15, sort=random");

but that's just randomly pulling 15 items from the first page, the one with id 1049.

How do I go about randomising both the pages and the repeater items as well?

Share this post


Link to post
Share on other sites

Hi @Tyssen.

for($i = 0; $i < 5; $i++):
$randomPage = $pages->find("your selector here")->findRandom(1);
foreach($randomPage->repeater_field->getRandom(3) as $item) :
				Output
		endforeach;
endfor;

Hope this helps.

Share this post


Link to post
Share on other sites

With

$randomPage = $pages->get("id=1049|1053|1055|1059|1152")->findRandom(1);

or getRandom(1) I get

Quote

Error: Exception: Method Page::findRandom does not exist or is not callable in this context

 

Share this post


Link to post
Share on other sites

Please try

$randomPage = $pages->find("id=1049|1053|1055|1059|1152")->findRandom(1);

Don't use get.

Gideon

Share this post


Link to post
Share on other sites

I then get an error on the next line.

Quote

Call to a member function getRandom() on null

And in testing whether $randomPage was actually returning anything by just outputting the page title, I'm getting

Quote

Page A
Page A
Page B
Page C
Page C

instead of repeater items randomised from Pages A–E.

Share this post


Link to post
Share on other sites

I got your idea wrong. Let me see....

$randomPages = $pages->find("your selector here")->shuffle();
foreach($randomPages as $randomPage):
foreach($randomPage->repeater_field->findRandom(3) as $item) :
				Output
		endforeach;
endforeach;

Use findRandom() instead of getRandom().

  • Like 1

Share this post


Link to post
Share on other sites

Try:

$result = new PageArray();
foreach($pages->getById([1049,1053,1055,1059,1152]) as $p) {
    $result->add($p->repeater_field->findRandom(3));
}
$result->shuffle();
// Now do something with $result - foreach, etc.

 

  • Like 1

Share this post


Link to post
Share on other sites

Thanks guys, I think I have it working, but I'm waiting for feedback from the content editor because I'm unsure if the output is as expected.

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...