cpx3 Posted May 2, 2022 Posted May 2, 2022 Hi there, to display a single slide from a huge database I only need a single page. But when I use the selector $pages->findOne("template=template-news,start=1,sort=created")->title; the selector seems to ignore the start position and always returns the same page. As the docs specify clearly that findOne has the same options as find, I have been trying to find my mistake for the last four hours with any success... (I cannot use find as this slows down things extremly). Any ideas?
August Posted May 2, 2022 Posted May 2, 2022 Hi cpx3, if I understand correct, alternate you could try display the first 3 titles like this:$news = $pages->find("template=template-news,start=1,sort=created")->slice(0, 2); // slice(start, limit) foreach($news as $i){ echo $i->title; } first() would be also an option, getting the needed page -I think...
BillH Posted May 2, 2022 Posted May 2, 2022 If a find() is too slow, use findMany() with a suitable selector: https://processwire.com/api/ref/pages/find-many/. Or if that's still too slow, or if you find them preferable for some other reason, use findJoin() or findRaw(), as described in the blog post at https://processwire.com/blog/posts/find-faster-and-more-efficiently/. Another option would be to try RockFinder3: https://processwire.com/modules/rock-finder3/.
Robin S Posted May 2, 2022 Posted May 2, 2022 (edited) You can do a find() but with a limit of 1. This will be just as efficient as findOne(), the only thing is that it will return a PageArray so use first() to get the item as a Page. $results = $pages->find("template=template-news, sort=created, limit=1, start=1"); if($results->count) { $title = $results->first()->title; } Edited May 2, 2022 by Robin S Corrected code 1
cpx3 Posted May 3, 2022 Author Posted May 3, 2022 Thanks to everybody for the answer! I tried findRaw which seems to be the most effective but brings me to another problem: How to access page references? I have the following code: $items = $pages->findRaw("template=template-news, select_region=austria, limit=2, objects=1", ["select_countries" => [ "id", "title" ]]); where select_countries are referenced pages. But I have no clue how to access the select_countries id as it neither works with array[0] nor with select_countries->title...
kongondo Posted May 3, 2022 Posted May 3, 2022 4 hours ago, cpx3 said: How to access page references? I have the following code: $items = $pages->findRaw("template=template-news, select_region=austria, limit=2, objects=1", ["select_countries" => [ "id", "title" ]]); where select_countries are referenced pages. But I have no clue how to access the select_countries id as it neither works with array[0] nor with select_countries->title... See if this topic helps? I've had issues with that findRaw page reference syntax in the past; unsure if it has been fixed in later PW versions as discussed in that topic.
BillH Posted May 3, 2022 Posted May 3, 2022 It seems that, as @kongondo notes, there's an issue with the findRaw() syntax, at least with the array-type syntax and page-reference fields. However, the dot syntax seems to work. So I think the following will give you what you need: $items = $pages->findRaw("template=template-news, select_region=austria, limit=2, objects=1", ["select_countries.id", "select_countries.title"]); Or slightly simpler, as the keys of the returned array of page references are the page ids: $items = $pages->findRaw("template=template-news, select_region=austria, limit=2, objects=1", ["select_countries.title"]); I've submitted an issue regarding the other syntax: https://github.com/processwire/processwire-issues/issues/1563 2
cpx3 Posted May 4, 2022 Author Posted May 4, 2022 Thanks once more for the answers! My problem is that I have no idea how to get the value of select_countries.title (which is a referenced page). I tried (beside many variations): foreach ($items as $p) { echo $p->select_countries.id; } But with no results...
BillH Posted May 4, 2022 Posted May 4, 2022 Using findRaw() you get a normal PHP array (not a PW page array). And the value for select_countries is itself an array. So you can do this: foreach($items as $item) { foreach($item['select_countries'] as $country) { echo $country['title']; } } I'm not sure what findRaw() returns if your page selector field is set to a single page, but you may need to simply use $item['select_countries']['title'] rather than the inner foreach. 1
kongondo Posted May 4, 2022 Posted May 4, 2022 1 hour ago, BillH said: Using findRaw() you get a normal PHP array (not a PW page array). True. Unless one adds the flag objects=1, in which case one get a stdClass ?. 2
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now