Fields empty but not really - does not render

I have a very basic and strange question / error.
I have the slug of a page, I look into my site using ->search() function.
I found the page but then when I try to render the title() for example it’s still empty, event that I have some in the content. Same other fields, I cannot render the url() or slug() or description()…

Here is the code of the template :

<?php elseif($item->category() == "projet"): ?>
      <?php
      $projetslug = $item->related_projet();
      $projetobj = $site->find('artistes')->search($projetslug);
      ?>
      <p style="height: 45px; width: 300px; border: solid blue 1px"><?=$projetobj?></p>
      <p style="height: 45px; width: 300px; border: solid red 1px"><?=$projetobj->title()?></p>
<?php endif ?>

In the blue box I can print my slug or objet and in the red box I try to print the title but nothing append.
I check the objet using toArray() and var_dump and the page is full of content.

Here is a screenshot of the resultat :

This will return a pages collection, not a single page, so calling title or any other page method here will fail.

Use

$projetobj = $site->find('artistes')->index()->find($projetslug);

instead.

But make sure this returns a page object before you call title, url etc.

Thank you.
Now I have a PHP error : Call to a member function title() on null.

I also try with :

$projetobj = $site->find('artistes')->index()->search($projetslug);

But I have the same problem as my last post.

That’s what I wrote above, you have to make sure that find returns a page:

<?php
if ($projetobj) {
  echo $projetobj->title();
}

That is a general rule that you have to keep in mind when working with non-static class methods.

1 Like

Yes I know that and I use it in other situation. But it does not fix my problem.
In this page (which is the home page), I found : artistes/agora/golden-ratio
Like I found it in many others pages of the website. So why here it does not work ?

Is artistes a first level page, i.e a direct child of the content folder?

Yes

Here is a screenshot of the structure.

On the right side is the projet.fr.txt that I try to render (Golden Ratio is the name of the project, Agora is the name of the artist).

What sort of field is related_project? If that’s a pages field, there is no need to get the page via the find or search but just

$projetobj = $item->related_projet()->toPage();
if ($projetobj) {
  echo $projetobj->title();
}