Sort Entry Listing

sort_hack.png

On the forums, someone recently commented on how the listings in MT could be improved with greater sorting flexibility. I thought that it was a very good idea and so after a bit of hacking managed to get the entry listings to be sorted, it would be easy to adapt this for the comments and trackbacks listing.

This hack will add ascending and descending sorting capabilities to the title and date fields. The rest you can use the Filter option for.

First, you'll need CMS.pm, around line 2411 in the list_entries sub, find the following

my %arg = (
'sort' => 'created_on',
direction => $sort_direction,
($limit eq 'none' ? () : (limit => $limit + 1)),
($offset ? (offset => $offset) : ()),
);

and replace it with

my $sort_type = $q->param('sort_type') || 'created_on';
my $sort_order = $q->param('sort_order') || 'descend';
$param{sort_type} = $sort_type;
$param{sort_order} = $sort_order;
my %arg = (
'sort' => $sort_type,
direction => $sort_order,
($limit eq 'none' ? () : (limit => $limit + 1)),
($offset ? (offset => $offset) : ()),
);

The next few changes apply to the list_entry.tmpl file found in tmpl/cms/. First of all find

<MT_TRANS phrase="Title">

and replace it with

<a href="<TMPL_VAR NAME=SCRIPT_URL>?__mode=list_entries&blog_id=<TMPL_VAR NAME=BLOG_ID>&sort_type=title&sort_order=ascend<TMPL_IF NAME=IS_POWER_EDIT>&is_power_edit=1</TMPL_IF>" title="<MT_TRANS phrase="Ascending">">&#x25B2;</a> <MT_TRANS phrase="Title"> <a href="<TMPL_VAR NAME=SCRIPT_URL>?__mode=list_entries&blog_id=<TMPL_VAR NAME=BLOG_ID>&sort_type=title&sort_order=descend<TMPL_IF NAME=IS_POWER_EDIT>&is_power_edit=1</TMPL_IF>" title="<MT_TRANS phrase="Descending">">&#x25BC;</a>

Next, find

<MT_TRANS phrase="Date">

and replace it with

<a href="<TMPL_VAR NAME=SCRIPT_URL>?__mode=list_entries&blog_id=<TMPL_VAR NAME=BLOG_ID>&sort_type=created_on&sort_order=ascend<TMPL_IF NAME=IS_POWER_EDIT>&is_power_edit=1</TMPL_IF>" title="<MT_TRANS phrase="Ascending">">&#x25B2;</a> <MT_TRANS phrase="Date"> <a href="<TMPL_VAR NAME=SCRIPT_URL>?__mode=list_entries&blog_id=<TMPL_VAR NAME=BLOG_ID>&sort_type=created_on&sort_order=descend<TMPL_IF NAME=IS_POWER_EDIT>&is_power_edit=1</TMPL_IF>" title="<MT_TRANS phrase="Descending">">&#x25BC;</a>

Next find

<input type="button" class="button" onclick="window.location='<TMPL_VAR NAME=SCRIPT_URL>?__mode=list_entries&blog_id=<TMPL_VAR NAME=BLOG_ID>&<TMPL_IF NAME=IS_POWER_EDIT>is_power_edit=1&</TMPL_IF>limit=' + this.form.limit.options[this.form.limit.selectedIndex].value + '&offset=<TMPL_VAR NAME=PREV_OFFSET_VAL><TMPL_VAR NAME=FILTER_ARGS>'" value="<MT_TRANS phrase="Previous [_1]" params="<TMPL_VAR NAME=LIMIT>">">

and replace it with

<input type="button" onclick="window.location='<TMPL_VAR NAME=SCRIPT_URL>?__mode=list_entries&blog_id=<TMPL_VAR NAME=BLOG_ID>&sort_type=<TMPL_VAR NAME=SORT_TYPE>&sort_order=<TMPL_VAR NAME=SORT_ORDER>&<TMPL_IF NAME=IS_POWER_EDIT>is_power_edit=1&</TMPL_IF>limit=' + this.form.limit.options[this.form.limit.selectedIndex].value + '&offset=<TMPL_VAR NAME=PREV_OFFSET_VAL><TMPL_VAR NAME=FILTER_ARGS>'" value="<MT_TRANS phrase="Previous [_1]" params="<TMPL_VAR NAME=LIMIT>">" />

Finally find

<select name="limit" onChange="window.location='<TMPL_VAR NAME=SCRIPT_URL>?__mode=list_entries&blog_id=<TMPL_VAR NAME=BLOG_ID>&<TMPL_IF NAME=IS_POWER_EDIT>is_power_edit=1&</TMPL_IF>limit=' + this.options[this.selectedIndex].value + '<TMPL_VAR NAME=FILTER_ARGS>'">

and replace it with

<select name="limit" onchange="window.location='<TMPL_VAR NAME=SCRIPT_URL>?__mode=list_entries&blog_id=<TMPL_VAR NAME=BLOG_ID>&sort_type=<TMPL_VAR NAME=SORT_TYPE>&sort_order=<TMPL_VAR NAME=SORT_ORDER>&<TMPL_IF NAME=IS_POWER_EDIT>is_power_edit=1&</TMPL_IF>limit=' + this.options[this.selectedIndex].value + '<TMPL_VAR NAME=FILTER_ARGS>'">

Your final change will be to edit the stylesheet. Add the following to it.

.list table th a {
color:#FFF;
text-decoration:none;
}

Upload all files to their respective locations and you should now be able to sort the title and date fields both ascending and descending. Post a comment if you experience any problems.

10 Comments

elise said:
on Jan 9, 2005 12:40 PM | Reply

Arvind, that's awesome! I put in a feature request a while back for the exact same thing. Since some of my blogs I use more as a CMS than as a blog (the recipe site for example) it would be much easier to find entries that need editing if they could be listed in alpha order rather than by date. Thank you!

Aziz said:
on Jan 11, 2005 4:07 AM | Reply

Thanks alot arvind, that was exactly want I need. I really appriciate your works. I hope that our friend in SA impeliment your hack to the MT. I really like to see most of you hacks in the original distribution.

Elise said:
on Jan 29, 2005 9:00 AM | Reply

Hi Arvind, Well, I'm finally getting around to implementing this and am having some problems. First, the section of the CMS.pm you mentioned above doesn't exist in my CMS.pm (vers MT 3.14). The closest thing to it is on line 2142:

lib/MT/App/CMS.pm

line 2142

my %arg = ( 'sort' => 'created_on', direction => 'descend', ($limit eq 'none' ? () : (limit => $limit + 1)), ($offset ? (offset => $offset) : ()), );

Second, what are we supposed to do with the list_entry.tmpl? The styles.css is a completely different file in the mt-static directory.

Arvind Satyanarayan said:
on Jan 29, 2005 9:11 AM | Reply

Line numbers may differ, the important thing is the code, and that is the correct code you have found that you need.

Just grab a copy of the list_entry.tmpl file that I have linked above and upload it to your tmpl/cms/ directory and add the code above to your stylesheet.

elise said:
on Jan 29, 2005 10:59 AM | Reply

Ah, naruhodo (Japanese for "now I understand").

Thanks, it works! Domo domo.

Christian Watson said:
on Feb 5, 2005 9:45 AM | Reply

Arvind, this will be a great help to us with out intranet installation of MT. One question: will this work with MT 2.64?

Arvind Satyanarayan said:
on Feb 5, 2005 5:01 PM | Reply

I just scanned MT 2.64's CMS.pm and the code changes in that file are exactly the same, the only difference is line numbers. Over here it looks to be around line 1441.

I'm going to update the tutorial above to reflect the changes one makes to list_entry.tmpl so you can apply it to any MT version!

Christian Watson said:
on Feb 8, 2005 11:18 PM | Reply

I implemented it for my personal blog before trying it out on our main work site. However, although the sorting tools display okay, clicking on them doesn't do anything! Any idea why? I'm using MT3.15.

gulliver said:
on Sep 22, 2005 5:14 PM | Reply

Damn, 3.2 seems totally different. I really want to be able to list alphabetically - suggestions appreciated.

Varium said:
on May 18, 2006 4:18 PM | Reply

Here is a link with the necessary code updates to do the same sorting feature in 3.2 including the Category column.

Click here to download code.