6 Ways to Customize WordPress Post Order
Recently, reader Luke Knowles asked how to customize the sort order of his posts in WordPress. Looking into a solution to this question proved quite enlightening. Within moments I was able to discern 4 methods for modifying post order, and then several days later I discovered 2 additional custom sorting techniques. After updating the reply to Luke’s comment, it seemed like some good information that other WordPressers may find useful. So, here are six ways to customize the sort order of posts in WordPress..
The Old Fashioned Way
To customize post order manually, open your theme document and use the following code:
// display posts organized by title in ascending order
<?php $posts = query_posts( $query_string . '&orderby=title&order=asc' ); ?>
<?php if( $posts ) : ?>
<div class="post">
<h1>Ordered by Post Title (Ascending)</h1>
<?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
<h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
<p><?php the_content(); ?></p>
<?php endforeach; ?>
</div>
<?php endif; ?>
This code is designed to display the specified number of posts in ascending order, according to post title. This code is quite generalized, and may be customized in countless ways. For example, you may modify the order in which the posts are displayed by modifying the orderby
and order
parameters of query_posts()
(in the first line). There are many useful values from which to choose, including category_name
and showposts
, thereby enabling virtually unlimited post-order customization.
Another useful post-ordering trick involves customizing the post order of, say, categories only, as one might do in an archive or service directory. To limit custom ordering to only categories, replace the first line of the previous loop example with this:
<?php if (is_category()) { $posts = query_posts( $query_string . '&orderby=title&order=asc' ); } ?>
..and then add another endif
statement to the end of the loop like this:
<?php endif; ?>
<?php endif; ?>
Of course, it is trivial to restrict post ordering to virtually anything — just replace is_category()
in that first line with the is_whatever()
statement of your choice:
is_home()
is_single()
is_author()
is_search()
is_archive()
..etc. For more, check out the WordPress Codex. Okay, that’s enough of that method, let’s move on to some easier custom-sorting methods..
Plugin #1: Custom Query String (Reloaded)
Custom Query String Reloaded is one of the most commonly used WordPress plugins. Custom Query String (CQS) provides a Admin Options panel whereby users may specify any number of custom post queries. CQS enables custom sorting of many different types of queries, including:
- archive
- author
- category
- date
- year
- time
- search
- home
..as well as individual categories, feeds, and several others. For each of these query types, users may customize the sort order by date, category, title, or author — in either ascending or descending order. This is a remarkable, highly flexible plugin that has served me well on a number of WordPress-powered sites.
Plugin #2: Smart Sort Plugin for WordPress
Smart Sort actually enables your visitors to select the order in which posts are displayed. Post-order options are presented to visitors as a “sort bar”, which includes a list of customizable ordering options. The sort bar may be placed anywhere within the document template, while displayed sorting options are controlled via the plugin’s WP Admin panel. Although I have not yet tried this plugin personally, it definitely sounds promising.
Plugin #3: aStickyPostOrderER
Although the current version (0.2.2.7) only works for WP 2.3 or better, the AStickyPostOrderER plugin enables users to customize post order according to category, tag, or the entire set of posts. Via the plugin’s comprehensive Options panel, users may customize the sort-order of any or all posts within a specific category or tag. Additionally, users may specify a generalized order in which posts from any category or tag are displayed. For example, you could customize your post order so that:
- Post #11 and #22 appear first in the Firefox tag archive
- Post #33, #44, and #55 appear first in the WordPress category
- Post #66, #77, and #88 appear first in the Mac category
- 5 posts from the News category always appear before all other posts
- All posts with the Ads tag always appear after the custom-sorted posts
- All posts in the Random category always appear after everything else
And this is just off the top of my head. Clearly, aStickyPostOrderER is an ideal plugin for highly customized tag, category, and overall post ordering. A useful plugin for helping people transform WordPress into a full-blown CMS.
Plugin #4: WP-Snap Plugin
As described at the plugin’s homepage, “WP-SNAP! (WordPress System for Navigating Alphabetized Posts) creates an alphabetical listing of post titles on a Category or Page template file.” With WP-SNAP!, users may customize the sort order of posts in category or page views by returning the post query in alphabetic order. Displaying posts in alphabetic post order is perfect for sites featuring directories, glossaries, indices, and other types of reference content. WP-SNAP! provides three unique navigational settings, works great with permalinks, and provides several other useful features.
If you are running WordPress 2.1 or better and are looking for an easy way to alphabetize your post order and provide easy “A-to-Z” navigation, WP-SNAP! may be the perfect solution.
Plugin #5: Sort Category Posts by Title
Although I have yet to try it, this plugin looks like a quick and easy way of sorting category posts in ascending alphabetical order according to post title. According to the plugin’s homepage, WordPress Sort Category Posts By Title was designed for WordPress 2.0.2 and only customizes the sort order of posts displayed in category view — it will not effect the ordering of posts seen elsewhere (e.g., the home page) in the site. The code for this plugin seems clean, simple, and just begging for further exploration and adaptation ;)
Wrap it up..
Regardless of your specific custom-post-ordering needs, one of these methods is sure to fill the suit. While this list is by no means comprehensive, it definitely should get you going in the right direction. Please let me know if you can think of any methods/plugins that are not on the list. — Peace!
37 responses to “6 Ways to Customize WordPress Post Order”
Hello
I am trying to do something very simple: every post I want
in chronological order, oldest first, newest last (or reverse chronological order if you prefer that term)
I can achieve this my using the stickyorder plugin, but then I have
for every new post again to change the order.
smartsort I can get to for I tried to add the
for example in footer.php like
<?php bloginfo('name'); ?> is proudly powered by <a href="http://wordpress.org/" rel="nofollow">WordPress</a>
but it does not appear.
You also mentioned the old fashion way which would be fine for me
but into which php file i have to insert the code.
I am running WP 2.6.1.
thanks
Uwe Brauer
@Uwe Brauer: the “old-fashioned way” should be applied to any WordPress loop for which you would like to reverse the display order. In typical themes, you would edit the loops found in the
index.php
,single.php
, and any other loop-containing files.I appreciate the discussion on sorting posts. I’ve got a problem though, I’m building a massive e-commerce site for a client and need to sort the products with the wp-ecommerce plug-in. Since the products aren’t posts, I’m not sure that the same code will work. Got any ideas?
Hi there,
I’m trying to do seemingly-impossible tasks. I am building an old family archive online and need to be able to use dates from 1867-1868 which presents a whole set of issues. I am also trying to get the posts to display in chronological order. Is there anyone that can help? I am running the latest WordPress version. Thank you, Elena Coen
elenacoen@gmail.com
@Jeff Starr,
well it did not really work,
I inserted the code into wp-content/themes/default/index.php
Since this file starts with the following lines:
I thought it would be best to insert it right after the start of that if.
No result.
I deleted the line
Nothing
I deleted even the
And the
Nothing again.
Could you please tell me where to insert the code.
Thanks
Uwe Brauer
it seems that lines of php I mentined in my previous post are not displayed, last try
I deleted the first line
and then the start of the if and its end.
Uwe
@Uwe: I see WordPress has been gobbling up all of your code! Remember to wrap single-line snippets in
<code>
tags, and multiple-line snippets in both pre and code tags:<pre>
<code>
. And of course, you are always welcome to simply send an email instead, they don’t eat code like WordPress does! ;)Fantastic article! Thank you! You are a life saver!
My pleasure, Oibalf — glad to be of service! Thanks for the positive feedback :)
Thank You, this was a lifesaver. When using WP as a CMS conditional posting is a necessity.
Hurray for Alphabetical Listings!
This is a great resource for WP programmers. Thank you.
Let me ask about AJAX. I am looking for an ajax solution to sorting functionality much like the smart sort offers (sorting by custom field, etc). I will be hardcoding several sorting options for the users to use in a top nav menu.
I am building a new property rental listing site for a client and he has requested these features. I spose it’s not a huge deal but it would be nice. Any incite appreciated.
Thank you for this wonderful post! I’m creating a new theme and searching for method of sorting posts. This helps me so much.