Jump to content

Check if PageArray contains pages


marcin
 Share

Recommended Posts

Hello,

I want to check whether PageArray contains some list of pages - as input I would like to use an array of pages' IDs. There is some elegant way to do it or I need to loop through PageArray and use $p->has("id=X")?

Thanks,

Marcin

Link to comment
Share on other sites

@Soma nice.

The problem may be if we want to match 10 IDs like this and let's say one of them is in the array,

->has would return true? Because '|' denotes an OR operator.

So if you want to know exactly which ID is in there, the loop is necessary.

Maybe I'm wrong :)

Link to comment
Share on other sites

That's what I assume he wants...

If you want to know which is in the page array use filter.

$found = $pageArray->filter("id=1001|1003|1005");
echo $found; // 1001|1003
  • Like 6
Link to comment
Share on other sites

That's what I assume he wants...

If you want to know which is in the page array use filter.

$found = $pageArray->filter("id=1001|1003|1005");
echo $found; // 1001|1003

Awesome!

How could I miss this in the Cheatsheet... again learned something new, thanks!

Link to comment
Share on other sites

Well that doesn't really come through until now... :D

So what now? If all of the "list" are in the PageArray (but also some others), or if the PageArray is the list 1:1... ?

Now I'm curious why you want or need that?

I would do that if you want to test if all of the list pages are in the PageArray:

$result = $pages->find("template=basic-page");
$list = $pages->getById('1001,1002');

if(count($result->filter("id=$list")) == count($list)){
    echo "all found";
}
 
  • Like 2
Link to comment
Share on other sites

Yeah it's not (yet) on the Cheatsheet. No it does grab the pages by id as the name indicated :)

ProcessWire never does grab the "whole" pages unless you need it or a field is autojoin. It's kinda lazy loading.

  • Like 1
Link to comment
Share on other sites

@Soma: I have attributes set as pages. So I have list of all attributes(list of pages under Attributes page) and Page inputfield in some template. Now, I want to search only these ones(from this template) which have selected all of attributes I choose.

You idea seems to be quite nice thanks. :)

Link to comment
Share on other sites

  • 4 weeks later...

OK guys, problem is probably a bit more complicated - @Soma's solution won't work if I want to search for some list of pages' IDs in multiple page field(not through all pages in system). It's a part of quite complicated search, so list of items is processed a few time - using filter() is prefered, multiple loops would probably decrease performance.

Example: I have list of pages: $itemsList - each item has attributes field(it's a page field). There is also array of attributes' IDs - I want to filter items which have all of attributes in attributes field.

Thanks,

Marcin

Link to comment
Share on other sites

Since you are dealing with the contents of a Page reference field, already loaded in memory (it sounds like?), take a look at the WireArray section of the cheatsheet, which outlines several methods you can use. Be sure to show the "advanced" options in the cheatsheet, because there is a lot in there. Take a stab at it and post what you've got. From there, I think we'll have a better idea of what you are trying to do, as well as a solution, if you can't get it working. 

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...