Jump to content

Selector to find all different authors


matjazp
 Share

Recommended Posts

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...