# Adding pages to a page field programmatically

**URL:** <https://forum.getkirby.com/t/adding-pages-to-a-page-field-programmatically/31339>\
**Category:** Questions\
**Created:** [April 9, 2024, 7:13pm UTC](https://forum.getkirby.com/t/adding-pages-to-a-page-field-programmatically/31339 "2024-04-09T19:13:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ShibaInu](https://avatars.discourse-cdn.com/v4/letter/s/bc8723/32.png) [@ShibaInu](https://forum.getkirby.com/u/ShibaInu)\
**Post date:** [April 9, 2024, 7:13pm UTC](https://forum.getkirby.com/t/adding-pages-to-a-page-field-programmatically/31339/1 "2024-04-09T19:13:37Z")

</div>

Hello everyone,

In my application users should have the possibility to favor certain pages. The user has received a “favorites list” in the yaml:

```auto
fields:
  favorites:
    type: pages

```

How can I add a page (via permalink) to this field? I have tried many things, but unfortunately none of them worked.

My last miserable attempt:

```auto
$user = $kirby->user();
$favorites= $user->favorites()->toPages();
$favorites->add(page('page://3hgC7B6br799ZJNC'));
$string = "";
  
foreach ($favorites as $page) {
    $string .= "- " . $page->permalink();

    if (!next($favorites)) {
        $string .= "\n";
    }
}

$user->favorites()->value($string);

```

Thank you very much for your help!  
Best regards

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [April 9, 2024, 7:25pm UTC](https://forum.getkirby.com/t/adding-pages-to-a-page-field-programmatically/31339/2 "2024-04-09T19:25:17Z")

</div>

I assume you want to update the favorites field in the user content here? Then you need to update the $user object ($user-\>update()), see [$user-\>update() | Kirby CMS](https://getkirby.com/docs/reference/objects/cms/user/update).

---

<div class="post-metadata">

**Author:** ![ShibaInu](https://avatars.discourse-cdn.com/v4/letter/s/bc8723/32.png) [@ShibaInu](https://forum.getkirby.com/u/ShibaInu)\
**Post date:** [April 9, 2024, 8:23pm UTC](https://forum.getkirby.com/t/adding-pages-to-a-page-field-programmatically/31339/3 "2024-04-09T20:23:56Z")

</div>

Thank you very much for your answer.  
Unfortunately, this does not work quite as expected.  
I can update the “test” field, but not my favorites list.

```auto
$favorites = $kirby->user()->favorites()->toPages();
$favorites->add(page('page://3hgC7B6br799ZJNC'));

$kirby->user()->update([
    'favorites' => $favorites,
    'test' => '123'
]);

```

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [April 9, 2024, 9:07pm UTC](https://forum.getkirby.com/t/adding-pages-to-a-page-field-programmatically/31339/4 "2024-04-09T21:07:48Z")

</div>

Because you need to store an array of page ids or uuids, not a collection of pages.
