Jump to content

Selector for parent status published


Hari KT
 Share

Recommended Posts

Hi guys,

I have a category template and articles in different category ( articles template ).

category1
  - articles
category2
  - articles
category3
  - articles
  - articles

I am listing all the articles via the selector "template=articles" . When the category is made as unpublished or hidden then also the articles are displaying. Is there a way other than doing

$category = $pages->find("template=category");
$category_ids = $category->__toString();
$articles = $pages->find("template=articles, parent=$category_ids");

I can get only the articles for published ones?

Thank you

Link to comment
Share on other sites

Try this...quick and dirty...works for me...could be better/smarter/other ways... :-)

$articles = $pages->find('template=articles, parent.status!=2049');

2049  = unpublished status...

Edited by kongondo
  • Like 2
Link to comment
Share on other sites

If you want to make it a little cleaner, you can do:

$articles = $pages->find("template=articles, parent.status<".Page::statusUnpublished);

Now you know when looking back on your code, what status you are looking for. Also, I changed the operator from != to < because the numbers that drive these status values are bitwise and are added to together. This way you could also have hidden applied to the category and it would work as expected, but if you use the != you'll get the articles returned again, because the status value will be the addition of unpublished and hidden.

Here are the values:

https://github.com/ryancramerdesign/ProcessWire/blob/6cba9c7c34069325ee8bfc87e34e7f1b5005a18e/wire/core/Page.php#L76

Note that 2049 is the addition of statusOn and statusUnpublished. Throw statusHidden in there as well and you end up with 3073.

  • Like 3
Link to comment
Share on other sites

Just as side note:

$category_ids = $category->__toString();

You never need to do this, toString is a magic method that does automatic if you write:

$category_ids = $category;

Or simply

$categories = $pages->find("template=category");
$articles = $pages->find("template=articles, parent=$categories");
Link to comment
Share on other sites

@Adrian....I am getting unknown selector operator errors with your code (even when I match the ' and ". )

Btw, I have always had problems using these constant statuses within selectors; I can never seem to recall (or find) the right syntax. You are right, they are much easier to read than status numbers!

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

×
×
  • Create New...