Latest TweetsDifference between mod_alias and mod_rewrite perishablepress.com/difference…
Perishable Press

6 Ways to Customize WordPress Post Order

[ Graphic: Inverted Triangular Fractal ] 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:

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!

Jeff Starr
About the Author Jeff Starr = Web Developer. Book Author. Secretly Important.
Archives
37 responses
  1. Sarah Hurty July 12, 2008 @ 7:33 am

    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.

  2. 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

  3. Jeff Starr

    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 ;)

  4. 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

  5. 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

  6. Jeff Starr

    @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

  7. Jeff Starr

    @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_posts tag. As for SEO concerns, assuming the plugin and the query_posts tag 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.

  8. 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!

  9. Jeff Starr

    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! :)

  10. 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:

    • Display a drop down menu on category & search pages similar to the ’sort by’ box that is used on YouTube.
    • Additional drop down list allowing the user the option of choosing how many posts per page they want to view.
    • Utilize WP’s built in sort by hooks. The reason for this is to make it as compatable with other plugins as possible.
    • Option to sort by any detected fields in the wp_posts table. (this allows for a modified posts table)
    • Option to sort by any custom fields.
    • Cacheable friendly

    Read his post on dyasonhat.com (404 link removed 2012/06/11)

    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.

  11. Jeff Starr

    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!

  12. Hi everybody,… Is there any way to sort the posts by the latest commented? let says “latest commented post”?
    Thanks

[ Comments are closed for this post ]