Jump to content

Best method for finding pages?


PWaddict
 Share

Recommended Posts

I display a button ONLY if there are more than 3 children on a specific template. So, I'm doing this:

$posts = $pages->find('template=posts-content');

if(count($posts) > 3) {
echo "Button";
}

On some point that template will have hundreds of children. Is the above method ok or should I use findMany?

$posts = $pages->findMany('template=posts-content');

if(count($posts) > 3) {
echo "Button";
}

or is it better to just use a limit on the selector?

$posts = $pages->find('template=posts-content, limit=4');

if(count($posts) > 3) {
echo "Button";
}

 

Link to comment
Share on other sites

Just a bit of an explanation for the next guy....:

4 hours ago, PWaddict said:

$posts = $pages->find('template=posts-content');

This will retrieve the pages, return a PageArray (PHP Object), which might end up using a lot of memory. Instead what you want is just the count of pages...

2 hours ago, adrian said:

$pages->count('template=posts-content');

Which brings us to this. This is very efficient. It will only count these pages and return an integer, not only doing its job very quickly but using next-to-nothing memory.

  • Like 7
Link to comment
Share on other sites

@Zeka, No is the answer

9 minutes ago, Zeka said:

$pages->find('template=posts-content')->count();

This will first return the PageArray (i.e. load pages) and then do an in-memory count. More like counting items in an array.

9 minutes ago, Zeka said:

$pages->count('template=posts-content');

This will do the counting at the db-level, hence faster and more efficient if all you want to do is count. 

  • Like 5
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...