Jump to content

[Solved] How to get pages without children using one template?


Remi
 Share

Recommended Posts

Hi!

I would like to build selector to get pages without children using one template.
 

// Doesn't work:
$selector = "template=invoice, children.template!=payment";


// Works but in opposite way I want:
$selector = "template=invoice, children.template=payment";


// Works, but it's useless because "invoice" can have other templates as children
$selector = "template=invoice, children.count=0";


// Code below works but it gets huge amount of data from DB first :/
$selector = "template=invoice";
$results = $pages->find($selector);
foreach ($results as $result) {
	$hasPayments = $result->hasChildren("template=payment");
	if (!$hasPayments){
		$content .= "Blablabla";
	}
}

 

Link to comment
Share on other sites

I'm on mobile, so I haven't tested this but can you try:

$pages->findMany("template=invoice")->not("children.template=payment");

It should find all invoices first, and then filter out the results of your second selector which you say is the opposite of what you want.

  • Like 1
Link to comment
Share on other sites

SOLUTION below

Yep. It works.

Corrected code:

$selector = "template=invoice";
$results = $pages->find($selector)->not("children.template=payment");

// or:
$results = $pages->find("template=invoice")->not("children.template=payment");

// Doesn't work correctly (doesn't exclude pages with children.template=payment (PW 3.0.62 & 3.0.96)):
$results = $pages->findMany("template=invoice")->not("children.template=payment");

It also executes 0,5-2 times faster than first working script.

I've tried to use findMany method, but it doesn't work with not method.

  • Like 1
Link to comment
Share on other sites

4 minutes ago, theo said:

Doesn't it simply work like this?


$selector = "template=invoice, !children.template=payment";

I've tried that before. It doesn't return any results :/

But when I use this selector:

$selector = "children.template!=payment";

I receive all pages without children using "payment" template.

Link to comment
Share on other sites

Humm it works here, but there is one thing to be aware of afaics:

As soon as there is "children.template=..." or "!children.template=..." in the second parameter, it does only list pages which actually have children.

$selector = "template=invoice" 

may list more pages than

$selector = "template=invoice, !children.template=atemplate";

even if none of the existing children have the "atemplate" template.

Link to comment
Share on other sites

6 minutes ago, theo said:

As soon as there is "children.template=..." or "!children.template=..." in the second parameter, it does only list pages which actually have children.

I think you are right.

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