6 Ways to Customize WordPress Post Order
by Jeff Starr on Tuesday, January 22, 2008 – 51 Responses
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
This plugin 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 otherwise default post order for the “Firefox” tag
- Post #33, #44, and #55 appear first in the otherwise default post order for the “WordPress” category
- Post #66, #77, and #88 appear first in the otherwise default post order for the “Macintosh” category
- Five posts from the “Important News” category always appear before all other posts
- All posts from the “Advertising” tag always appear after the custom-sorted posts
- All posts from the “Nonsense” tag 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. In my opinion, a key 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!
Focused on clean code and quality content, Perishable Press is the online home of Jeff Starr, author, artist, designer, developer, and all-around swell guy. 





51 Responses
Louis – #1
That’s a super great memo Jeff, Thank you.
Perishable – #2
Thanks Louis — glad you enjoyed it! Hopefully this post will help some people fulfill their custom post-ordering needs :)
M Sinclair Stevens – #3
Thank you so much for this explanation! I’ve been trying to change the display of one archive to be in ascending date order and in the sidebar have the index be in ascending alphabetical order.
With your help, I did it!
Thanks!
Perishable – #4
Excellent — thanks for the feedback :)
Steven – #5
Thanks very much for this, and especially for the pointer to aStickyPostOrderER - what a brilliant plugin. I use a static front page, on which I display an arbitrary selection of posts. Previously I was accomplishing this using the “Get A Post” plugin, which made a new set of DB queries for each post, obviously not optimal in terms of resource usage.
So instead I added the tag “front” to the posts I want to show, used a custom query_post string & loop to display only the posts with that tag, and then ordered them explicitly with aStickyPostOrderER. I saved 16 DB queries by comparison with the old method. :)
Perishable – #6
Yes indeed, aStickyPostOrderER is quite extraordinary — one of those plugins that provides far more functionality than anyone will ever actually need.
Hearing about your custom loop optimization process is very inspiring. In fact, during the next several days, I am preparing to completely dive into some serious site optimization, fine-tuning and general clean-up. The goal is to tie up all the loose ends so that I can finally move ahead with a fresh redesign. And I look forward to it — using aStickyPostOrderER opens up a whole new realm of layout possibilities. :)
Ricardo – #7
What if i want to order by date stored on other tables? Im using eventcalendar plugin. I need to sort the results by the Start date stored on the ec3 table. I know i can use a select custom query but it would brakes the pagination. Ive been trying to hack query.php to add the statement on the ORDERBY, but i dont know how. Any ideas on this?
Gracias!
Perishable – #8
Sounds like you tried some of the same things that I would have tried. Beyond those things, and a thorough Google search, I would download and investigate the functionality of the various plugins mentioned in this article. In the past, cannibalizing snippets of previous, open-source scripts has really proven helpful in revealing essential clues and solving difficult issues. Good luck! ;)
iPaulPro – #9
This may be the wrong place for this comment, but I’m taking a chance:
I’m looking for a wordpress solution that is similar to “Smart Sort” except it needs to actually work.
I would like to be able to offer sorting options to users (on all pages - especially the main index), with options like this:
One “bar” with viewing options:
View - Titles, Excerpts or Full Posts
and a second with sorting option:
Sort By - Title, Date, or Category
Are there any other efforts out there, that solve my desires.
Thanks all, great post btw.
Perishable – #10
Hi iPaulPro, you are definitely in the right place to leave such a comment, but I honestly can’t think of anything off the top of my head that would accomplish that list of specific tasks. I will, however, unleash the bloodhounds and keep a watchman on the tower just in case something happens to surface. If/when it does, I will report back here with the news.
iPaulPro – #11
Thanks for the quick response and look-out!
I am flirting with the idea of using jQuery to show/hide multiple (hidden) loops corresponding to the user’s request.
Being new to javascript and php, I’m fairly certain that this is not the most efficient way to do this.
My thoughts:
1. Create multiple loops in my main index with semantic css markup.
2. Set all loops to display:none besides the default (sorted by date, showing excerpts)
3. Create jQuery-powered buttons to toggle the appropriate loop (while staying aware of the current display and saving the new selected options)
4. Somehow figure out how to do this, having almost no javascript experience, and maintaining all wordpress functionality.
My other thought was to have different templates for each sorting option, triggered by jQuery buttons. This seems more scary to me, for some reason.
Basically, I want to be able to sort my post, like you would sort products in an online store.
All I’m asking, is if you think this is a logistical nightmare, or if I’m on the right track.
I feel that this type of ‘plug-in’ would be very useful and desirable. And really, I want it for my site (under construction - im a newb).
Would love to make it my first Wordpress contribution (i know, a little ambitious)
Thanks again for any response.
Perishable – #12
Hi iPaulPro, that sounds like a straightforward and useful implementation of jQuery. In fact, I’m not sure if you have already tried a search, but I remember seeing something very similar just a few weeks back. If I recall correctly, there were three loops, each represented by a tab in an upper, horizontal menu. The menu had tabs for each of three different loops/topics. Clicking on a tab revealed its topical loop while hiding the previous display. Personally, I didn’t see how this benefited the user; no clicks are saved in the process and users without JavaScript get a cluttered mess. Nonetheless, depending on how easy your plugin is for the average user, I could see it becoming fairly popular. Of course, you of all people will benefit the most from taking the time to learn about how to make a plugin! ;)
Sarah Hurty – #13
Thanks for this site. I would like to ask where to find plugins that work on wordpress version 2.5.1 ‘Post order’ in the side bar and in the content. Please take a look to my site how can you help me. I appreciate it.
Steev – #14
I just going to try Custom Query String but i wanna know from you that which one is best for customizing posts in alphabatic order waiting for you response
Regards
Perishable – #15
Hi Steev, three of the plugins listed in the article provide alphanumeric post-ordering functionality. Depending on your needs, either the CQS, WP-Snap, or Sort Cat Posts plugins would do the job. If it were me, I would try CQS first because it provides control over many other aspects of post queries. If that doesn’t work, then perhaps one of the others will do the trick. You may also want to search again for a new alphabetize posts plugin — at the rate they’re cranking ‘em out these days, I wouldn’t be a bit surprised ;)
iPaulPro – #16
Hi Perishable (and fellow readers)
I have not forgotten about this thread, just been busy try to finish my site and figure out a way to solve my desires (above comments #9 and #11).
Perishable, you said that you couldn’t see how this benefited the user. Perhaps I can explain a little better:
Say my main index is set to display the ten latest post’s excerpts sorted by date (newest first)
So you arrive at my blog’s main index. By default, you are shown the above configuration.
On that page i have the latest 4 posts in the category Wordpress, 2 in the category jQuery, 1 in the category Google, and 3 uncategorized.
Now, if you wanted to see only posts in Wordpress, that’s easy enough, you navigate to the Wordpress category page.
But what if you wanted to see the latest 20 posts that are in both the Wordpress and jQuery categories? And what if you wanted them sorted by title, instead of by date, and only showing titles and not excerpts?
How would you do this?
So my idea is to make a system (probably a form), where your posts are sortable (browse-able) by these factors, similar to the way you are able to refine your browsing parameters in an e-commerce store (eg. sort: by manufacturer, by price: low to high, display: 100 products per page) like this
Its important that the user be able to select multiple ‘filters’ at once. So you could see the latest 20 posts in the Wordpress category, sorted by title, displaying titles only, and do this with one ‘request’.
I have never seen this in a blog, and am not aware of any similar solutions available for Wordpress, so I am trying to figure it out myself.
So . . .
I have made some progress, but am falling very short of achieving a “e-commerce-like” ability to provide user-side sorting of my blog posts. Unfortunately, my site is not done, so I cannot yet show what I have. (sorry, dont have the time to set up a public test site)
Because of my limited php abilities, I have been focusing on jQuery click events to show/hide different loops. There are many ‘limitations’ to this approach (that I have yet to overcome): giving the user the ability to choose the number of posts per page - while maintaining wordpress’ paging system, caching the user’s choices, and others that are harder to explain in one sentence.
My site is built on the Wordpress Sandbox, which makes life easy and amazing. So its fairly simple for me to make an anchor (with jQuery) that could say, “hide all posts that aren’t in the category Wordpress (without reloading the page)”. All I do is make a click function to display:none (hide) any post that doesn’t have the semantic class markup “.category-wordpress”.
This is great, but the problem is, if you click this anchor on my main index, you will only be shown the latest 4 posts. I’d like the next 6 (in this case) to automatically ‘fill-in’ the rest of the page, while maintaining wordpress’ paging system. (i believe this is possible but am not there yet. I also may not use this method at all, and is only important when considering a post in 2 categories - I a hoping to create a user-submission system allowing multiple parameters)
I have achieved dynamically sortable lists of posts (by title) using the jQuery Tablesorter plug-in. This approach is very simple and straightforward, allowing me to make a table of posts, sortable by date, title, and category, but it does not solve many of the problems (like changing number of posts per page, or providing the option to see excerpts while maintaining these sorting options).
I am a far way off completing this mission and appreciate any help or insight. I think I am going to post in the Wordpress forums and stuff soon - I just haven’t had the time.
In conclusion (for now)
I really need to get my site done so I can focus on clients. For now, I will be implementing a bare-bones version of this “system” just to get the site done. When my site goes live, I will be sure to post here.
Wow - that turned out to be a lot longer than expected.
Thanks again Perishable, for the help and the great site.
@Steev -
If you only desire to show posts alphabetically, you need only the query_posts tag .
- Paul
Steev – #17
Thanks Perishable for your reply and please tell me one more thing that for SEO and site speed The Old Fashioned Way way is good or i would use a plugin and please tell me how to add Notify me of follow-up comments via email again, thanks for your precious advice in advance.
Thanks
Perishable – #18
@iPaulPro: wow is right! Thanks for taking the time to explain everything. I think your idea about implementing “e-commerce-like” functionality for blogs is ahead of its time. As time goes on, the need for efficient and flexible navigation, sortability, and organization of site content will greatly increase. The amount of content on this site alone is getting to be kind of scary, and I have been thinking about different strategies for organizing and presenting it in a usable and accessible way. I can’t say that JavaScript should be relied upon as the primary method by which such design is implemented, however, it could certainly play a useful role. Thanks again for taking the time to share your ideas with us. Definitely keep us in the loop as to your progress!
Regards,
Jeff
Perishable – #19
@Steev: personally, I try to rely on as few plugins as possible. If something can be done “The Old Fashioned Way”, as you say, then that is the method I prefer. As for “SEO and site speed,” I cannot say for certain, but I am sure that the CQS plugin isn’t going to destroy your site’s performance. In fact, unless you get into some serious testing, I doubt you will even notice a difference between using CQS and the
query_poststag. As for SEO concerns, assuming the plugin and thequery_poststag are generating the same output, there will be no SEO benefits one way or another. The structure and organization of your site and pages, however, will have an impact on how your site is ranked in the search engines.Moshu – #20
The link to CQS in the article above gives a 404 - the initial author doesn’t support it anymore.
There is a CQS Reloaded (v2.9) that works even with the latest WP version and it can be downloaded from my site.
Enjoy!
Perishable – #21
Hi Moshu, thanks for the information and also for supporting and improving the excellent CQS plugin. I have updated the article with the correct link — Thanks again! :)
iPaulPro – #22
Hey Jeff
I just thought I’d let you know that I finally found a plug-in that will fulfill most of my desires (explained above):
Funny thing is, its “SmartSort”, a plug-in you describe in this article. The maker of “SmartSort”, dyasonhat, recently posted an announcement of version 2.0 of SmartSort. These are some of the features he lists:
Read his post on dyasonhat.com
So it looks as though the limited work I did on this was in vain, but I learned a bunch! I can’t wait to see how it actually works.
Figured you’d be interested to hear.
BTW - I really like your new site design. My site is live, but I have been so busy that I have very little content right now. I’ll let you know when I implement SmartSort 2.0.
Thanks again for your help and support.
Jeff Starr – #23
Thanks for the update, iPaulPro! SmartSort was a great plugin to begin with, and now it sounds as if it is even better. It really does provide some useful functionality for customizing WordPress post order. Definitely let me know once you implement on your site — would love to check it out. Also, I am glad you like the new site design — thanks!
arismawan – #24
Hi everybody,… Is there any way to sort the posts by the latest commented? let says “latest commented post”?
Thanks
Uwe Brauer – #25
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
Jeff Starr – #26
@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.Jason Grover – #27
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?
Elena Coen – #28
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
Uwe Brauer – #29
@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
Uwe Brauer – #30
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
Jeff Starr – #31
@Uwe: I see WordPress has been gobbling up all of your code! Remember to wrap each line of code with
<code>tags, and do not use<pre>tags. You are always welcome to simply send an email instead, they don’t eat code like WordPress does! ;)Oibalf – #32
Fantastic article! Thank you! You are a life saver!
Jeff Starr – #33
My pleasure, Oibalf — glad to be of service! Thanks for the positive feedback :)
Ellis – #34
Thank You, this was a lifesaver. When using WP as a CMS conditional posting is a necessity.
Hurray for Alphabetical Listings!
studioLeland – #35
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.
Tuan Anh – #36
Thank you for this wonderful post! I’m creating a new theme and searching for method of sorting posts. This helps me so much.
Jeff Starr – #37
My pleasure, Tuan Anh — glad the article is useful for you :)
Trackbacks / Pingbacks