# Add() function not working

**URL:** <https://forum.getkirby.com/t/add-function-not-working/5255>\
**Category:** Questions\
**Tags:** v2\
**Created:** [September 14, 2016, 9:38am UTC](https://forum.getkirby.com/t/add-function-not-working/5255 "2016-09-14T09:38:51Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![daizumi](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/daizumi/32/11799_2.png) [@daizumi](https://forum.getkirby.com/u/daizumi)\
**Post date:** [September 14, 2016, 9:38am UTC](https://forum.getkirby.com/t/add-function-not-working/5255/1 "2016-09-14T09:38:51Z")

</div>

I’m using the `add()` function to add a hand-picked page to the front of a collection, as a highlighted page, based on [this example](https://getkirby.com/docs/cheatsheet/pages/add), but it still won’t work.

```
$highlight = (string)$site->find('home')->highlight(); //from a select field
$entries = $site->grandChildren()->visible()->without($highlight)->sortBy('date', 'asc');
$entries->add($highlight)->flip();

```

I get is my collection as usual, with an empty page at the end.

The `flip()` is there so that the added page ends up first, but it’s not even taking effect, so I guess the problem happens just before, at the `add()`.

Already tried supplying the `add()` with all possible sorts of things (page object, id, uid, etc).

---

<div class="post-metadata">

**Author:** ![Thiousi](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/thiousi/32/13656_2.png) [@Thiousi](https://forum.getkirby.com/u/Thiousi)\
**Post date:** [September 14, 2016, 10:00am UTC](https://forum.getkirby.com/t/add-function-not-working/5255/2 "2016-09-14T10:00:03Z")

</div>

Hello,  
Can you try this?

```auto
$highlight = page(page('home')->highlight());
$entries = $site->grandChildren()->visible()->without($highlight)->sortBy('date', 'desc')->append($highlight);

```

page() is similar to $site-\>find()  
append is similar to add but it does what you want straight away, it adds in the first position.

---

<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:** [September 14, 2016, 10:05am UTC](https://forum.getkirby.com/t/add-function-not-working/5255/3 "2016-09-14T10:05:50Z")

</div>

If you want to prepend the page, you can use `$collection->prepend()`:

```auto
$highlight = page(page('home')->highlight());
$key= page('home')->highlight();
$collection->prepend($key, $highlight);

```

Or maybe

```auto
$key= page('home')->highlight()->value();

```

to get a string.

---

<div class="post-metadata">

**Author:** ![daizumi](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/daizumi/32/11799_2.png) [@daizumi](https://forum.getkirby.com/u/daizumi)\
**Post date:** [September 14, 2016, 10:10am UTC](https://forum.getkirby.com/t/add-function-not-working/5255/4 "2016-09-14T10:10:21Z")

</div>

Great! 🙂 Thanks @Thiousi and @texnixe!

`$collection->prepend()` works best for my purpose. I just used `prepend(0, $highlight)` and avoided the $key value. Hope that’s a correct usage.

---

<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:** [September 14, 2016, 10:16am UTC](https://forum.getkirby.com/t/add-function-not-working/5255/5 "2016-09-14T10:16:17Z")

</div>

I guess the key should be the ID … as in [this example](https://forum.getkirby.com/t/cant-find-home-when-using-add-to-collection/5204) and the [docs](https://getkirby.com/docs/cheatsheet/pages/prepend):

BTW: `$collection->append()` uses the same syntax, so it should be:

```auto
$collection->append($key, $highlight);

```

Edit: And to clarify this:

- `append()` adds the page(s) at the end of the collection
- `prepend()` adds the page(s) at the beginning of the collection

---

<div class="post-metadata">

**Author:** ![daizumi](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/daizumi/32/11799_2.png) [@daizumi](https://forum.getkirby.com/u/daizumi)\
**Post date:** [September 14, 2016, 3:28pm UTC](https://forum.getkirby.com/t/add-function-not-working/5255/6 "2016-09-14T15:28:37Z")

</div>

Thanks for the clarification.

So $key is an arbitrary value then. If I want to, it can be different from the ID of the original page, right?

---

<div class="post-metadata">

**Author:** ![bnomei](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bnomei/32/775_2.png) [@bnomei](https://forum.getkirby.com/u/bnomei)\
**Post date:** [September 15, 2016, 10:35am UTC](https://forum.getkirby.com/t/add-function-not-working/5255/7 "2016-09-15T10:35:15Z")

</div>

> collection does only have `add()`. but pages inhering from collection has `append()` as well, right? so using `add()` on pages will not have the desired effect. `append()` should be used.

like @lukasbestle corrected me below… above is not right. collection has append and prepend. pages has add.

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [September 15, 2016, 10:38am UTC](https://forum.getkirby.com/t/add-function-not-working/5255/8 "2016-09-15T10:38:04Z")

</div>

It’s the other way around. `append` and `prepend` are methods in the `Collection` class, `add` is specific to `Pages` collections.

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [September 15, 2016, 10:39am UTC](https://forum.getkirby.com/t/add-function-not-working/5255/9 "2016-09-15T10:39:24Z")

</div>

> [@danieljacoby](#):
>
> So $key is an arbitrary value then. If I want to, it can be different from the ID of the original page, right?

It _can_, but to ensure compatibility it shouldn’t be. You should always use `$p->uid()` as key.

---

<div class="post-metadata">

**Author:** ![bnomei](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bnomei/32/775_2.png) [@bnomei](https://forum.getkirby.com/u/bnomei)\
**Post date:** [September 15, 2016, 10:41am UTC](https://forum.getkirby.com/t/add-function-not-working/5255/10 "2016-09-15T10:41:38Z")

</div>

you are right i will edit post above to avoid confusion.

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 4:55pm UTC](https://forum.getkirby.com/t/add-function-not-working/5255/11 "2025-02-06T16:55:15Z")

</div>


