matjazp Posted May 2, 2015 Posted May 2, 2015 Is there a better way of finding all different post authors? $posts = $pages->find("template=post"); $authors = []; foreach($posts as $p) { $authors[$p->createdUser->id] = $p->createdUser->get("title|name"); } Later on I need a selector like this: $now = time(); $posts = $pages->find("template=post, !post_expires<$now, limit=20, sort=-post_created"); So I have 2 (redundand) queries, so I would like to "extend" the first query (performance?): $newposts = $posts->find("!post_expires<$now, limit=20, sort=-post_created"); but that way pagination is not working.
tpr Posted May 3, 2015 Posted May 3, 2015 So your last query works ($newposts = ...), only pagination fails? What's your pagination code and how it fails exactly?
matjazp Posted May 3, 2015 Author Posted May 3, 2015 I use $posts->renderPager() method for pagination. $posts = $pages->find("template=post, limit=20"); $posts->getLimit(); returns 20 $posts->getStart(); returns 0/20/40 etc. $posts->getTotal(); returns 203 $posts->count(); return 20 $posts = $pages->find("template=post"); $newposts = $posts->find("limit=20"); $newposts->getLimit(); returns 0 $newposts->getStart(); returns 0 $newposts->getTotal(); returns 203 $newposts->count(); return 0
LostKobrakai Posted May 3, 2015 Posted May 3, 2015 It won't work because you're calling different methods even though they look the same. $pages->find() is calling Pages::find, while $posts->find() is calling PageArray::find. These are different in that the second one doesn't give you pagination values. You would need to set them by yourself, but it's not done automatically because having a PageArray suggests, that all pages are already loaded from the database.
tpr Posted May 3, 2015 Posted May 3, 2015 This topic may help: https://processwire.com/talk/topic/3384-search-with-merged-pagearrays-pagination-not-working/?p=42994
matjazp Posted May 3, 2015 Author Posted May 3, 2015 I have set $start = ($input->pageNum - 1) * 20; $newposts = $posts->find("limit=20, start=$start"); $newposts->setLimit(20); $newposts->setStart($start); and pagination (and rendering the posts) is working. But this is slower than having two "independent" queries: first $pages->find(...) just to get the authors and second $pages->find(...) to get the posts. Thank you LostKobrakai and tpr.
LostKobrakai Posted May 3, 2015 Posted May 3, 2015 As the number of posts grow it may become more efficient to just check each user for potential posts instead of loading all posts to memory and reading their createdUser field.
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