Latest TweetsWordPress and the Blank Target Vulnerability (aka rel noopener + noreferrer): perishablepress.com/wordpress-… #WordPress #security #html
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. Security Specialist. WordPress Buff.
Archives
37 responses
  1. That’s a super great memo Jeff, Thank you.

  2. Jeff Starr

    Thanks Louis — glad you enjoyed it! Hopefully this post will help some people fulfill their custom post-ordering needs :)

  3. M Sinclair Stevens January 25, 2008 @ 5:48 pm

    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!

  4. Jeff Starr

    Excellent — thanks for the feedback :)

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

  6. Jeff Starr

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

  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!

  8. Jeff Starr

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

  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.

  10. Jeff Starr

    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.

  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.

  12. Jeff Starr

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

[ Comments are closed for this post ]