# Two variables in one aca merge collections

**URL:** <https://forum.getkirby.com/t/two-variables-in-one-aca-merge-collections/8170>\
**Category:** Questions\
**Tags:** v2\
**Created:** [August 4, 2017, 12:20pm UTC](https://forum.getkirby.com/t/two-variables-in-one-aca-merge-collections/8170 "2017-08-04T12:20:06Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![valentin1](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/valentin1/32/12789_2.png) [@valentin1](https://forum.getkirby.com/u/valentin1)\
**Post date:** [August 4, 2017, 12:20pm UTC](https://forum.getkirby.com/t/two-variables-in-one-aca-merge-collections/8170/1 "2017-08-04T12:20:06Z")

</div>

Hi !

I’m building a website where, on the homepage, news and projects are mixed but organised by date, then for each category there is an attributed page ‘projects’ and ‘news’ which I achieved.

However for the homepage, I don’t really know how could I say : for each ‘project’ and ‘news’…

For the project I’ve written the variable like this :

```
<?php
$projects = page('projects')->children()->visible()->sortBy('date', 'desc');
?>

```

then I called it like this :

`<?php foreach($projects as $project): ?>`

To sum up, I would need something like :

```
   <?php
    $projects = page('projects' AND 'news')->children()->visible()->sortBy('date', 'desc');
    ?>

```

Thank you ! 🙂

---

<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:** [August 4, 2017, 12:25pm UTC](https://forum.getkirby.com/t/two-variables-in-one-aca-merge-collections/8170/2 "2017-08-04T12:25:42Z")

</div>

You can merge both collections and then sort by date:

Two options:

```auto
//Merge two collections
<?php 
  $projects= page('projects')->children()->visible();
  $news = page('news')->children()->visible();
  $combined = new Pages(array($projects, $news))->sortBy('date', 'desc');
?>

//Appending stuff to a collection:
<?php 
  $combined = new Pages();
  $combined->add(page('projects')->children()->visible());
  $combined->add(page('news')->children()->visible());
  $combined = $combined->sortBy('date', 'desc');
?>

```

---

<div class="post-metadata">

**Author:** ![valentin1](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/valentin1/32/12789_2.png) [@valentin1](https://forum.getkirby.com/u/valentin1)\
**Post date:** [August 4, 2017, 12:35pm UTC](https://forum.getkirby.com/t/two-variables-in-one-aca-merge-collections/8170/3 "2017-08-04T12:35:42Z")

</div>

Hi,

Thanks for your answer !

However I don’t really understand how to append the content of the var…  
Just for you to understand how I did (and how I just know how to do… I’m new here haha) here’s my snippet for projects-call.

```
<?php
$projects = page('projects')->children()->visible()->sortBy('date', 'desc');
?>

<?php foreach($projects as $project): ?>

<article class='article-projects' ?> )">

<a href="<?php echo $project->url() ?>">

			
			<h1><span><?php echo $project->title()->html() ?></span> </h1>
			<p lang= <?php echo $site->language()->code() ?>
	><?php echo $project->description2() ?> </p>
			
			
			<?php if($image = $project->image('cover.jpg')): ?>
			  <img src="<?php echo $project->image('cover.jpg')->crop(900, 600,100)->url() ?>" alt="">
			<?php else: ?>
			    <img src="<?php echo $project->image()->crop(900, 600,100)->url() ?>" alt="">
			<?php endif ?>
</a>
</article>

<?php endforeach ?>

```

Do you know If I could use the same syntaxe ?

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:** [August 4, 2017, 12:41pm UTC](https://forum.getkirby.com/t/two-variables-in-one-aca-merge-collections/8170/4 "2017-08-04T12:41:54Z")

</div>

Well, after you have merged/appended the collection, you loop through them like in the above example.

```auto
<?php 
  $projects= page('projects')->children()->visible();
  $news = page('news')->children()->visible();
  $combined = new Pages(array($projects, $news))->sortBy('date', 'desc');
?>
<?php foreach($combined as $project): ?>

<article class='article-projects' ?> )">

<a href="<?php echo $project->url() ?>">

			
			<h1><span><?php echo $project->title()->html() ?></span> </h1>
			<p lang= <?php echo $site->language()->code() ?>
	><?php echo $project->description2() ?> </p>
			
			
			<?php if($image = $project->image('cover.jpg')): ?>
			  <img src="<?php echo $project->image('cover.jpg')->crop(900, 600,100)->url() ?>" alt="">
			<?php else: ?>
			    <img src="<?php echo $project->image()->crop(900, 600,100)->url() ?>" alt="">
			<?php endif ?>
</a>
</article>

<?php endforeach ?>

```

---

<div class="post-metadata">

**Author:** ![valentin1](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/valentin1/32/12789_2.png) [@valentin1](https://forum.getkirby.com/u/valentin1)\
**Post date:** [August 4, 2017, 12:51pm UTC](https://forum.getkirby.com/t/two-variables-in-one-aca-merge-collections/8170/5 "2017-08-04T12:51:34Z")

</div>

Thanks ! it works. However it doesn’t work with the

`->sortBy('date', 'desc')`

… do you know why ?

🙂

---

<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:** [August 4, 2017, 12:55pm UTC](https://forum.getkirby.com/t/two-variables-in-one-aca-merge-collections/8170/6 "2017-08-04T12:55:43Z")

</div>

Oh, we have to change it a bit:

```auto
<?php
$projects= page('projects')->children()->visible();
$news = page('news')->children()->visible();
$combined = new Pages(array($projects, $news));
$combined = $combined->sortBy('date', 'desc');
?>

```

Or wrapped in parenthesis:

```auto
$combined = (new Pages(array($projects, $news)))->sortBy('date');

```

---

<div class="post-metadata">

**Author:** ![valentin1](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/valentin1/32/12789_2.png) [@valentin1](https://forum.getkirby.com/u/valentin1)\
**Post date:** [August 4, 2017, 1:00pm UTC](https://forum.getkirby.com/t/two-variables-in-one-aca-merge-collections/8170/7 "2017-08-04T13:00:13Z")

</div>

> [@texnixe](#):
>
> \<?php  
> $projects= page(‘projects’)-\>children()-\>visible();  
> $news = page(‘news’)-\>children()-\>visible();  
> $combined = new Pages(array($projects, $news));  
> $combined = $combined-\>sortBy(‘date’, ‘desc’);  
> ?\>

This is so great ! Thanks a lot !!

Now just a last one 😛

Is there a possibly to echo some stuff depending of the origin of the article ?  
Because the content of news and article are totally different but the snippet name are sometimes the same.

So can I say something like :

```
<?php if($project = $combined is page('news')->children() ?>

//ECHO STUFF FOR NEWS

<?php else if($project = $combined is page('project')->children() ?>

//ECHO STUFF FOR PROJECT

```

?? 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:** [August 4, 2017, 1:02pm UTC](https://forum.getkirby.com/t/two-variables-in-one-aca-merge-collections/8170/8 "2017-08-04T13:02:10Z")

</div>

```php
<?php
if($project->parent()->uid() == 'news') {
  // do stuff
} else {
  // do sth. else
}
?>

```

---

<div class="post-metadata">

**Author:** ![valentin1](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/valentin1/32/12789_2.png) [@valentin1](https://forum.getkirby.com/u/valentin1)\
**Post date:** [August 4, 2017, 1:03pm UTC](https://forum.getkirby.com/t/two-variables-in-one-aca-merge-collections/8170/9 "2017-08-04T13:03:29Z")

</div>

Thanks a lot !  
I learnt a lot thanks to you. 😉

Best greetings,  
Valentin

---

<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, 5:32pm UTC](https://forum.getkirby.com/t/two-variables-in-one-aca-merge-collections/8170/10 "2025-02-06T17:32:25Z")

</div>

Archived on February 6, 2025, 5:32pm UTC


