# Programmaticly update page with object field

**URL:** <https://forum.getkirby.com/t/programmaticly-update-page-with-object-field/26920>\
**Category:** Questions\
**Tags:** v3\
**Created:** [November 2, 2022, 8:57am UTC](https://forum.getkirby.com/t/programmaticly-update-page-with-object-field/26920 "2022-11-02T08:57:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![wouter](https://avatars.discourse-cdn.com/v4/letter/w/e99b99/32.png) [@wouter](https://forum.getkirby.com/u/wouter)\
**Post date:** [November 2, 2022, 8:57am UTC](https://forum.getkirby.com/t/programmaticly-update-page-with-object-field/26920/1 "2022-11-02T08:57:18Z")

</div>

I’m trying to update a page programmaticly using page()-\>update():

> **[$page-\>update()](https://getkirby.com/docs/reference/objects/cms/page/update)**
>
> Updates the page data

something like this:

```auto
page( $page->id() )->update([
    'title' => "test",
    ]);

```

with works very well. But I can’t figure out how to update an object field:

> **[Object](https://getkirby.com/docs/reference/panel/fields/object)**
>
> Structured object input, which stores properties as YAML.

I can’t seem to find any example so I tried a few things like wrapping the object fields in an array but no luck. Anyone out there who can point me in the right direction?

Thanks!

---

<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:** [November 2, 2022, 9:22am UTC](https://forum.getkirby.com/t/programmaticly-update-page-with-object-field/26920/2 "2022-11-02T09:22:01Z")

</div>

Could you please post what array you tried?

---

<div class="post-metadata">

**Author:** ![wouter](https://avatars.discourse-cdn.com/v4/letter/w/e99b99/32.png) [@wouter](https://forum.getkirby.com/u/wouter)\
**Post date:** [November 2, 2022, 9:35am UTC](https://forum.getkirby.com/t/programmaticly-update-page-with-object-field/26920/3 "2022-11-02T09:35:44Z")

</div>

Yes. I thought simple and plain like this:

```auto
page()->update([
  'myObject' => [
    'title' => "test",
    ]
  ]);

```

---

<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:** [November 2, 2022, 9:48am UTC](https://forum.getkirby.com/t/programmaticly-update-page-with-object-field/26920/4 "2022-11-02T09:48:09Z")

</div>

That should work and works in my little test. What do you get as result? Is the object field defined in the blueprint of the page you are trying to update? And does the subfield `title` exist?

---

<div class="post-metadata">

**Author:** ![wouter](https://avatars.discourse-cdn.com/v4/letter/w/e99b99/32.png) [@wouter](https://forum.getkirby.com/u/wouter)\
**Post date:** [November 2, 2022, 10:36am UTC](https://forum.getkirby.com/t/programmaticly-update-page-with-object-field/26920/5 "2022-11-02T10:36:20Z")

</div>

Yes, i did define them in the bleuprint and it seems to work there:

```auto
          rheader:
            type: object
            fields:
              rtitle:
                type: text

```

shows me this in the content file:

`Rheader: rtitle: ok`

when i then try to change it:

```auto
  page( $page->id() )->update([
    'rheader' => [
      'rtitle' => "test",
      ]
    ]);

```

nothing seems to change. if i change other (not object fields) it works great.

one thing that might be problematic is that i call the update function in a “file.create:after” hook because i would like to get specific contents from that file and store it in the object. so i do need to reload the page manually for the fields to change. could that be the problem?

---

<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:** [November 2, 2022, 10:42am UTC](https://forum.getkirby.com/t/programmaticly-update-page-with-object-field/26920/6 "2022-11-02T10:42:45Z")

</div>

> [@wouter](#):
>
> could that be the problem?

I don’t know. Yes, you need to reload for the changes to reflect in the Panel, but the field content in the page’s content file should nevertheless change.

Let me try from a hook.

On a side note: Why don’t you just use `$page->update()` instead of trying to fetch the page you already have with `page($page->id())`?

Ok, I tried from a hook, and the content file is updated, but of course the change does only reflect in the Panel after reloading the page.
