Listing Entries from Multiple Movable Type Blogs, Sorted by Date

| 9 Comments | No TrackBacks |
Movable Type has a pretty neat set of features called 'MultiBlog' which allow you to display content on your blog that is coming from other blogs on your installation (aggregation...).  That is all very nice if all you want to do is show a combined list of the most recent entries of (for example) blog 1, 2 and 5 (just do <mt:entries blog_ids="1,2,5">...</mt:entries> and you are done).  But what if you want more control over the entries that are being displayed?
An example
Suppose you want the entries in category "Sports" from one blog, the entries tagged "soccer" and "tennis" from another blog and all entries from a third blog (which is about nothing but baseball anyway).  All ordered by date and just the ten most recent ones please.

It would be nice if you could do <mt:entries blog_ids="1,2,5" categories="1:sports" tags="2:soccer,tennis" lastn="10"> or something similar, but this is not how it works.  Does that mean it is impossible?  Of course not, this is Movable Type!

The solution
Use an <mt:setvarblock> tag to capture enough entries from all three sources in a hash, where the date/time is the value.  Then sort by the value and display the first ten items you get.

<mt:entries blog_ids="1" category="Sports">
<mt:setvarblock name="theentry"><li><mt:entrydate> <mt:entrytitle></li></mt:setvarblock>
<mt:setvarblock name="entries{$theentry}"><mt:entrydate format="%Y%m%d%H%M%S"></mt:setvarblock>
</mt:entries>

<mt:entries blog_ids="2" tag="soccer OR tennis">
<mt:setvarblock name="theentry"><li><mt:entrydate> <mt:entrytitle></li></mt:setvarblock>
<mt:setvarblock name="entries{$theentry}"><mt:entrydate format="%Y%m%d%H%M%S"></mt:setvarblock>
</mt:entries>

<mt:entries blog_ids="5">
<setvarblock name="theentry"><li><mt:entrydate> <mt:entrytitle></li></mt:setvarblock>
<mt:setvarblock name="entries{$theentry}"><mt:entrydate format="%Y%m%d%H%M%S"></mt:setvarblock>
</mt:entries>


<ul>
<mt:loop name="entries" sort_by="value numeric reverse">
<mt:if name="__counter__" lt="11"><mt:var name="__key__"></mt:if>
</mt:loop>
</ul>



No TrackBacks

TrackBack URL: http://www.movabletips.com/cgi-bin/mt/mt-tb.cgi/38

9 Comments

Hi,

I came across this post from google looking for some way to sort entry by title.
I know you can use MTEntries sort_by="title" but this modifier prioritizes capitalized letters first to the sort. Im not sure if this is a glitch in the system but this modifier should sort by alphabets used in the title.

Example:

I would like to sort these titles:
-APRICOT
-Aligator
-ABBEY
-Apple

If MTEntries sort_by="title" is used:
-ABBEY
-APRICOT
-Aligator
-Apple

But it really should be
-ABBEY
-Aligator
-Apple
-APRICOT

Would you know how to this?

This was spectacularly useful to me. I'd been giving myself a bit of a headache trying to come up with just this logic. Thank you!

useful to me. I'd been giving myself a bit of a headache trying to come up with just this logic

Thanks for this, took me a while to figure out where to insert everything (which is the main reason I never had a blogroll in the first place, but whatever) but it's working fine now (except that now I need to rebuilt all my old entries.. and unless I do it a few at a time it crashes but that's another problem..)

Thanks for sharing this great tip!

I am facing very stupid problem. I have created a widget 'popular entries' which is being displayed on blog page and blog's categories page. On blog page, it displays 5 entries as expected but on blog's categories page, it just show 1 entry as it seems to be taking context of blog category.

Code is like this,


title -

As the code is common and I have specified multiple blogs as context so why does it show only 1 entry on blog category page. It should show 5 entries there as well.

Am I doing something wrong here?

I totally can't get this to work. For some reason the lt="11" isn't limiting the entries to the last 10 (or any number) at all, and it isn't ordering them by date properly. It's just spitting them out through the loop in the order that they were placed. Any ideas? Thanks.

Nevermind - the reason it won't work is because there's an error in third block of code in the example (which just happened to be the one I grabbed). It's missing the

I used an mt:setvarblock tag in order to capture enough entries from all three sources in a hash, and it works, thank you for your advise

Your method of explaining the whole thing in this piece of writing is really pleasant, every one be capable of without difficulty understand it, Thanks a lot.

Leave a comment


Movable Type Help?

"Can my Movable Type issue or project be taken care of by a professional?" Yes It Can Be! Contact YesItCan.be

Tags

Powered by Movable Type 5.2.2