Forum

Thread tagged as: Question

Returning one repeater item with a checkbox in a multiple item region

I have a multiple item region, each with a repeater. I would like to use perch_content_custom to return one repeater item across the whole region at random, but only one that has a checkbox checked.

At the moment I am looping through the arrays that perch_content_custom is returning and matching against the checkbox field. Then using PHP rand to select one.

Is there a more efficient way to do this?

Creative Monster

Creative Monster 0 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

You should be able to filter the region on repeater.field having a checked value to only return items that include a repeater with a checked item.

So that gives me items that have a repeater with at least one checkbox ticked?

Is there then a way to filter only the items within the repeaters that have a checkbox checked?

Right so I've managed to do that that last bit now too using another template, only now I need to select at random an item within the repeater.

Drew McLellan

Drew McLellan 2638 points
Perch Support

I don't think we have anything to do that.

Okay thanks Drew. I've just done it using php for now suing the result from perch_content_custom. Just in case there's something to consider for a future update my use-case is:

  • There is a testimonials page where there are sets of testimonials grouped by company
  • Each group is an item within an multiple item region
  • Within each item is a repeater for the list of testimonials from that company
  • I want to pull at random one testimonial from the whole page to use on another part of the site
  • And I want to give the website owner the option to decide which ones get shown at random in the other section, so each repeater item includes a checkbox

Hello,

I just wanted to check if I've understood something in here correctly.

Using perch_content_custom() with a filter to match against a repeater will return all items in the repeater, not just the one that it matches? And other than looping through the array with PHP you cannot get just that one field?

I'm just checking as it's a problem I'm currently facing and am just writing a PHP loop to go through and only display what is needed, just want to make sure I am doing it right.

Thanks, Neil.

Hi Neil

You can return just one field using filter but I was trying to return only one field with a specific value.