Jump to content

Getting page parents, ignoring hidden, to use in breadcrumbs/hierarchy


AAD Web Team
 Share

Recommended Posts

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

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)

 

  • Like 3
  • Thanks 1
Link to comment
Share on other sites

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

  • Like 4
Link to comment
Share on other sites

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.

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