<p>I finally got this working, here is how, I'm sure there is a better way or ways I can improve, but for now it works.</p>

<p>First, we need to create an empty array like so...</p>

<pre><code>{% set posts = [] %}
</code></pre>

<p>Then we need to loop through the entries, and for each entry create an array containing the <code>entry.postDate</code> and the entry data. We then need to push that <code>entry</code> array into the main <code>posts</code> array</p>

<pre><code>{% for entry in craft.entries.section('blog').limit(10) %}

    {#-- We store the date reference for our custom sort function, and the entry data --#}
    {% set entry = [{
        'date': entry.postDate,
        'data': entry
    }] %}

    {#-- Now we push that entry array into the posts array --#}
    {% set posts = posts|merge(entry) %}

{% endfor %}
</code></pre>

<p>So now we've got an array containing just our craft entries. Next we need to install the Dukt <a href="https://dukt.net/craft/twitter" rel="nofollow noreferrer">Twitter plugin</a> and the Dukt <a href="https://dukt.net/craft/oauth" rel="nofollow noreferrer">oAuth Plugin</a>. Set them both up via the documentation and then we move on to the next step.</p>

<p>We now need to pull in our tweets, which with these two plugins is surprisingly easy. It looks pretty much the same as the above chunk of code, except we're executing a GET request to Twitter instead of pulling in the entries from Craft. Also, because the tweets <code>created_at</code> variable is formatted in a non-standard way, we need to reformat it into the same format as the craft entry date.</p>

<pre><code>{% for tweetRef in craft.twitter.get('statuses/user_timeline', {
    count: 10,
}) %}

    {% set tweet = [{
        'date': tweetRef.created_at|date('Y-m-d'),
        'data': tweetRef
    }] %}
    {% set posts = posts|merge(tweet) %}

{% endfor %}
</code></pre>

<p>So now, our posts array contains both our entries and tweets, but not in date order. With a bit of research I discovered <a href="https://craftcms.stackexchange.com/questions/870/sort-merged-array-by-array-value-in-twig">this post</a> which explains Twig can't sort multi-dimensional arrays. Luckily @victor_in in that post provided a solution. He has developed a plugin with improves twigs sort functionality. So download his plugin, then as he mentioned, modify the plugin to add the custom sort function. </p>

<p>This is the custom sort function that I used. </p>

<pre><code>private function sortByDate($a, $b)
{
    return strtotime($a['date']) &lt; strtotime($b['date']);
}
</code></pre>

<p>I should also note that Craft was giving me an error with <code>array('TwigBetterSortTwigExtension', 'sortByDate')</code> saying the class 'TwigBetterSortTwigExtension' not being found. I fixed this by replacing it with <code>array(__CLASS__, 'sortByDate')</code>.</p>

<p>So now, we can loop through our posts array using the custom sort filter to order them by the date.</p>

<pre><code>{% for entry in posts|sort('dateSort') %}
    {{ entry.date }}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2015-06-12T14:19:16.633Z
+1 for the self-answer! Thanks for this post, it's helping me too.
Neal Magee
CC BY-SA 3.0
2014-10-17T15:54:57.253Z
2014-10-17T15:54:57.253Z
2017-04-13T12:51:00.200Z
KeironLowe
<p>I'm trying to build what's basically a blog page, but spread throughout the posts we want some of our tweets as well. Obviously we have the entries from Craft, and I've found this <a href="https://dukt.net/craft/twitter" rel="nofollow">plugin</a> which I can use to pull in our tweets.</p>

<p>My question is how can I merge our entries and tweets and order them by date? </p>

<p>I was thinking of creating an array with the entries, then pulling in the tweets and merging them into the array and then ordering them by date.</p>

<p>Is this possible?</p>

<entries><array>
Entries & Twitter Posts
# Entries & Twitter Posts

(Asked by: username_0 on 2014-10-17)

I'm trying to build what's basically a blog page, but spread throughout the posts we want some of our tweets as well. Obviously we have the entries from Craft, and I've found this [plugin](https://dukt.net/craft/twitter) which I can use to pull in our tweets.

My question is how can I merge our entries and tweets and order them by date?

I was thinking of creating an array with the entries, then pulling in the tweets and merging them into the array and then ordering them by date.

Is this possible?

**Tags:** entries, array

---

## 1 answers

### Answer by username_0 on 2014-10-17. Score: 5 | Accepted answer

I finally got this working, here is how, I'm sure there is a better way or ways I can improve, but for now it works.
First, we need to create an empty array like so...
{% set posts = [] %}

Then we need to loop through the entries, and for each entry create an array containing the entry.postDate and the entry data. We then need to push that entry array into the main posts array
{% for entry in craft.entries.section('blog').limit(10) %}

    {#-- We store the date reference for our custom sort function, and the entry data --#}
    {% set entry = [{
        'date': entry.postDate,
        'data': entry
    }] %}

    {#-- Now we push that entry array into the posts array --#}
    {% set posts = posts|merge(entry) %}

{% endfor %}

So now we've got an array containing just our craft entries. Next we need to install the Dukt Twitter plugin and the Dukt oAuth Plugin. Set them both up via the documentation and then we move on to the next step.
We now need to pull in our tweets, which with these two plugins is surprisingly easy. It looks pretty much the same as the above chunk of code, except we're executing a GET request to Twitter instead of pulling in the entries from Craft. Also, because the tweets created_at variable is formatted in a non-standard way, we need to reformat it into the same format as the craft entry date.
{% for tweetRef in craft.twitter.get('statuses/user_timeline', {
    count: 10,
}) %}

    {% set tweet = [{
        'date': tweetRef.created_at|date('Y-m-d'),
        'data': tweetRef
    }] %}
    {% set posts = posts|merge(tweet) %}

{% endfor %}

So now, our posts array contains both our entries and tweets, but not in date order. With a bit of research I discovered this post which explains Twig can't sort multi-dimensional arrays. Luckily @victor_in in that post provided a solution. He has developed a plugin with improves twigs sort functionality. So download his plugin, then as he mentioned, modify the plugin to add the custom sort function. 
This is the custom sort function that I used. 
private function sortByDate($a, $b)
{
    return strtotime($a['date']) < strtotime($b['date']);
}

I should also note that Craft was giving me an error with array('TwigBetterSortTwigExtension', 'sortByDate') saying the class 'TwigBetterSortTwigExtension' not being found. I fixed this by replacing it with array(__CLASS__, 'sortByDate').
So now, we can loop through our posts array using the custom sort filter to order them by the date.
{% for entry in posts|sort('dateSort') %}
    {{ entry.date }}
{% endfor %}

> **Comments:**
> **username_1**: +1 for the self-answer! Thanks for this post, it's helping me too.  

---

