Check if page exists based on something other than URI

Thanks for expanding @texnixe. I always sit and watch your posts for 5 minutes after you’ve said something incase you elaborate :slight_smile:

Poor thing :hugs: Sorry, yeah, I always have these afterthoughts…

Thank you both for your input. I have a solution in place based on your help, but the original question is… still in question. Why didnt’ my original code work… that second bit that @texnixe pointed out?

Consider this:

$pages->find('attorneys')->children()->findBy('title', $pg->author())->title();

The above throws an error that reads, “Call to a member function title() on boolean”

The following, however works as expected.

$pages->find('attorneys')->children()->findBy('title', 'Thomas E. Brenner'))->title();

You are in a foreach loop, so not every item in the foreach loop will return a page and thus throw an error; never use a method if you haven’t made sure the object exists first (edit: I think I need a shortcode/text expander for this sentence :wink:)

if($p = $pages->find('attorneys')->children()->findBy('title', $pg->author())) {
  echo $p->title();
}

I gotta come clean and admit that I’m not exactly sure what you mean in regards to this case.

Please explain further or just tell me to die quietly in a corner. :persevere:

So you’re saying that each request of…

$pages->find('attorneys')->children()->findBy('title', $pg->author())->title()

may not result in something because $pg->author() may not be set?

Even this below…

if($pg->author()->isNotEmpty()): echo $pages->find('attorneys')->children()->findBy('title', $pg->author())->title(); endif;

generates the same error.

I don’t understand why you don’t want to do it the way I suggested in the last post :thinking:.

This line:

echo $pages->find('attorneys')->children()->findBy('title', $pg->author())->title()

(with the title() method at the end) will fail if the attorney page has been deleted or never existed. You have to first check if that page exists, then call the title method.

So the whole snippet should look like this:

// check if there is something in the author field
if($pg->author()->isNotEmpty()): 
  // if so, check if there is a page with the value of this field as title
  if($p = $pages->find('attorneys')->children()->findBy('title', $pg->author())):
    // hurray, the page exists! Echo the title…
    echo $p->title();
  endif;
endif;

The first if statement is not even necessary, if the field is empty, the page check will return false, anyway.

@texnixe thanks for your patience and additional detail.

I used your snippet. I do not receive any errors, but it never finds a page based on $pg->author().

I know for certain that there are cases where the value of $pg->author() and a attorney page title match exactly.

It seems silly, but can you confirm that you can use a variable in findBy()?

Try the below to see whats actually in $p and tell us the result. You might to do a view source on the page to read it properly as it might be in one long line on the page. Should be ok in actual page source in the browser.

// check if there is something in the author field
if($pg->author()->isNotEmpty()): 
  // if so, check if there is a page with the value of this field as title
  if($p = $pages->find('attorneys')->children()->findBy('title', $pg->author())):
    var_dump($p);
  endif;
endif;

$page->author() is what is not working, please use $pg->author()->value().

THAT’S IT!

So I needed $pg->author()->value()

$field->value()

Yeah, sorry, I had thought about that earlier but then thought that had been fixed in the meantime. For me it’s not consistent that you can pass a field to filterBy() but not to findBy().