Custom Article Sorting with TextPattern

I first want to mention that this is incredibly easy to implement and doesn’t require any TextPattern hacking. I learned of this tip from textpattern.org.

The Situation

I recently had a client that wanted control over the order articles were displayed. I prefer to sort articles by Title, Posted Date, or Last Modified Date because it’s a logical hierarchy. However, depending on how you’re using TextPattern, it may not always make sense to sort articles in methods previously listed.

In my client’s case, the website was going to have articles that would act as static pages within sections. Here’s an example of the section/page structure:

  • About Us
    • Mission Statement
    • Company Information
    • Values

We have the section (which is also the root page) “About Us” and under this we have three pages, “Mission Statement”, “Company Information”, and “Values”. To set this up in TextPattern we’re going to create four articles, each corresponding with the four pages I just mentioned.

If you look at the example above, the pages aren’t in alphabetical order, so we can’t sort by title. We could’ve create the pages in order and sort by posted date, but it’s not always likely that pages are created in the order they’ll be displayed.

So How Do I Sort Them?

I created a custom field called “sort_order” and enter the number of the order the article should appear. The root page, “About Us” has a sort_order value of “1”, “Mission Statement” has a sort_order value of “2”, and so on. Then I use the following TextPattern snippet to display the articles:

<txp:article_custom sort="custom_3 asc" section="About Us" form="output_navigation" limit="999" />

There’s no TextPattern magic here, it’s taking the value from the sort attribute and plugging it into the SQL query. This means you could sort entries by just about any field. Keep in mind that if you’re going to sort using this method, you have to use the name of the field from the database table, and not the custom field name.

Commenting is closed for this article.

Current Entry

This journal entry was posted on April 29, 2008 at 03:59 AM. It is filed under Tutorial. There are 0 comments. View the archives. Subscribe to the RSS feed.