AAD Web Team Posted August 11, 2022 Share Posted August 11, 2022 We're trying to get all the parent pages of a page in order to display a breadcrumbs trail on the web page. We're using: $page->parents('template!=home') … to get all the non-home parents ready to display. We didn't want to include hidden pages in the parents list, and based on the selector documentation I thought they would be excluded, but the hidden pages are returned in the list. Is this how it's supposed to be? In order to not retrieve the hidden pages we also tried: $page->parents('template!=home,status!=hidden') … but this still returned the hidden parent pages. Can anyone help with this? Is the documentation wrong, or am I misunderstanding it? We're using ProcessWire 3.0.184. Link to comment Share on other sites More sharing options...
bernhard Posted August 11, 2022 Share Posted August 11, 2022 I don't see that the docs say anything different: https://processwire.com/api/ref/page/parents/ You can use parents("status=1"), but that might also exclude other pages (like pages having errors after save). Or you do a foreach and check $page->isHidden() for every item. 2 Link to comment Share on other sites More sharing options...
Jan Romero Posted August 11, 2022 Share Posted August 11, 2022 A page’s status is a bit field technically you need to check whether it contains the hidden status. You can do this with ProcessWire selectors using the bitwise AND operator and negating the result: $page->parents('id!=1, !status&1024') Instead of the number 1024 you can also use the named constant: $page->parents('id!=1, !status&' . Page::statusHidden) 3 1 Link to comment Share on other sites More sharing options...
Robin S Posted August 11, 2022 Share Posted August 11, 2022 @AAD Web Team, if you're wondering why status!=hidden doesn't work in this selector it's because status keywords like that are only supported in PageFinder selectors like $pages->find() that query the database, and not by selectors that match against a PageArray in memory. And although the docs don't make this clear $page->parents() actually adds all the parents one by one into a PageArray and then filters that PageArray by the selector. See here. It is a bit confusing that $page->children($selector) goes through PageFinder but $page->parents($selector) doesn't. 4 Link to comment Share on other sites More sharing options...
AAD Web Team Posted August 11, 2022 Author Share Posted August 11, 2022 Thanks @bernhard and @Jan Romero. I didn't think of how status works as a bit field, and I've never used the bitwise 'or' operator in a selector. The code snippet with Page::statusHidden is very useful, thank you. The part of the documentation I found confusing was in the selectors documentation where it says, "Pages with hidden or unpublished status will not appear in the results from database-querying selectors that can return multiple pages (i.e. functions that return the PageArray type). For instance $pages->find(), $page->children(), etc." However, I understand it now. Thanks @Robin S for explaining the underlying logic to why $page->children() and $page->parents() work differently with hidden pages. I can see from your linked post how this has been discussed previously. Unfortunately, in my forum searching before asking this question I didn't come across your post – so thanks for pointing me to it. 3 Link to comment Share on other sites More sharing options...
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