It all started a couple of days ago, when we were working on an aquarium fish website, and we had some trouble while trying to sort posts within certain categories alphabetically.
We found a plugin called Sort Category Posts By Title that seemed to be just what we needed, but when using it only with specific categories, it wouldn’t work if pretty permalinks were activated.
We also tried Sobek’s Posts in Category, but found out that, even though it still gets about 10 downloads each day, it does not work for WordPress 2.6.3 or higher.
Fortunately, about that time we stumbled upon the Custom Queries page in the Codex, the ‘encyclopedia of WordPress knowledge’. That’s where we found out that there exists a hook called ‘posts_orderby’ designed to do exactly what we needed.
So we figured that since we’re planning to be using more of WordPress in the future as a general purpose Content Management System, we could also contribute by writing this much needed post sorting plugin.
You can download via the official WordPress plugins repository.
To-Do List
- Sorting for author pages
- Sorting for tag pages
- Sorting for the main blog page
- Sort by custom fields
107 Comments
Would it be possible with this plugin to do such a tricky sorting:
1) sort posts tagged with one special word alphabetically (a-z),
2) sort the rest (or tagged with another special word) alphabetically (a-z) based on the last word of the title.
It would be a great help for my open project.
…the idea is I’ve got both people (first name second name) and institutes (just name) so I’d like people to be sorted with their last name, which is sorta… last in the title.
Hi there, pp.
Well, first of all, a much simpler solution to your problem would be to create two separate child-categories, one for people and one for institutions.
And secondly, yes the type of sorting you are suggesting would be a bit tricky. So, while we will consider adding this feature in a future release, we can’t promise anything.
Good luck with your project,
Gabriel
The thing is, our sorting method is closely tied to the MySQL sorting.
Thanks for such a prompt reply. The reason I ask for this functionality is that the site is kind of a index, where a user can pick for example “psychotherapy for families” + “a city” and the system shows both individual psychotherapists and psychotherapy centers in the same listing — hence the idea to mix both types of sorting. I understand it is probably very specific system…
Great plugin – just what I was looking for. Been hunting high and low for a plugin as simple as this that works with 2.7 so great job. Thanks again. Dare to Share
Could you turn this into a “Post of the day” plugin that would randomly pull a post from a category and display using the “Excerpt” in 2.7?
Hmmm… Please elaborate a little. Where would that “Post of the day” thing occur? Instead of the regular category display? If so, how would the user be able to see all the posts?
Would be great to see one post displayed randomly in the sidebar on a daily basis. It could pull a random post a day from a specific category or tag to display with a title, the excerpt, and a link (or the title could be the link to the original post). I think it would be a great way to give users a chance to see random or older posts on a daily basis that they might not normally go searching for. Also, it could be used as a ‘quote of the day” plug which can pull from a category. Think it can be done?
Yeah, it’s a great idea for a plugin (or a widget, actually).
But I’m sorry to say that it’s not what our plugin is for. As it’s name suggests, our plugin is for sorting posts, and we’d like to keep it as simple as possible. Apparently this is what people are looking for.
When it comes to WP plugins, swiss army knives don’t seem to be very popular.
I’ve installed this plug-in on a blog in WPMU. The basic Default sort drop-down appears, but the categories option is not populated. I suspect this is due to the fact that in WPMU, each blog has it’s own unique taxonomy table, unlike the single table in WP.
Any idea if there is a work-around for this? (We’re running WPMU 2.6.3)
Thanks
Brian
So how do plugins work for WPMU ? Do you install them for all users, or just for one user?
In fact both – but in this instance, we have only enabled it for a single blog.
Oh, I see, you have installed it on a single blog in WPMU.
You can try to edit the plugin and replace all instances of wp_tablename with wp_”n”_tablename, where “n” is the blog ID.
Maybe that will work.
I’d more or less arrived at that conclusion, andI may do this as a quick fix, but ideally I’d like to have the plug-in automatically become aware of which blog it is in, and adjust the table names dynamically to suit. I’ll need to dig into the WPMU Codex to find out how this is done. If I find out, I’ll post the answer back here.
Thanks
Brian
Cool. Thanks.
We will definitely do something about the WPMU thing sometime in the future.
Cheers
Gabriel.
So what about sorting post within categories on tags? You think this is possible?
yup, it’s clearly possible, although a bit tricky.
it would be very useful. it’s been on our to-do list from the beginning.
so, now i’m appending the to-do list to the post.
Hi-
First, GREAT plugin! Just what I was looking for… Nothing too complex, nor too simple. Thank you for this!
Second, I was having some performance issues on site. I installed a tuner plugin, and it returned that there is an error in the SQL query for this plugin. I thought I’d run it by you to get your thoughts…
The text of the error is:
6) Query: [wp-content/plugins/wp-post-sorting/wpps_hook_posts_orderby.php(14): wpdb->get_row()]
SELECT * FROM wp_post_sorting WHERE category =
Error in query:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ” at line 1
Query time: 0.4821 (ms)
Nevermind… I just figured out why the error…. Noob mistake. It was because I was on the index page where there *are* no categories.
Thanks again for an awesome plugin!
You know, I found and installed that “WP Tuner” plugin, (which is great by the way) and I also got that SQL error. It apparently was a minor bug, but now it’s fixed.
If I have another kid, I think I’ll name him Gabriel.
Thanks!
LOL:))
Hi Gabriel,
I use your plugin and love it. One question I have. When I do WP_Query in my home.php for a specific category, it does not use the sort order specified for the category that was entered from WP Post Sorting plugin.
Is there a way I can make a query to leverage the settings that WP Post Sorting has for a particular category. In other words. Something that would replace this query and use the settings for the category:
have_posts()) : while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID;
?>
Sorry.. that query didn’t display too well…. anyway, the heart of the query is:
WP_Query(category_name=events)
Hello Ray,
I’m afraid to say I don’t know of any way you can do it.
When is the next version coming out?
I think it should have the custom fields and also a
tag that allows users on the website to sort posts.
Two drop downs would be nice, one having all the chosen custom
fields as well as title, and date… and the second one where the
user can select the order: decs, asc… and if u really wanna go all
out… have it so you can control everything! =)
Nice plugin by the way.
Thanks
Yeah, the new version will definitely have sorting by custom fields, but unfortunately, this requires rewriting the entire plugin from scratch, and we’re a bit tied up at the moment.
I am not getting this to work. any suggestions?
I am going to need a bit more info. What WordPress version are you using? Are you sure you activated the plug-in? Did you go to Posts >> Post Sorting? Is the category list there populated or not? Did you get any errors? Stuff like that.
Wanted to thank you for this awesome and simple plug-in; on my blog, I have categories for movie/book/game reviews, and it is awesome to be able to show only those categories sorted by title!
In my particular case, I wonder if it is possible to exclude the word “The” from sorting when it appears at the beginning of the title — for example, so that a title like “The Lord of the Rings” would sort under L instead of T.
It’s certainly possible, but it would take a fair amount of work. And unfortunately we’re a bit tied up at the moment.
This might sound silly, but where does it sort posts? where is the sort displayed?
And, most, importantly, why do I have to ask this question?
Hey, Ric
I think you might find this screenshot quite relevant.
Cheers, Gabriel
Hi Gabriel,
Thanks for developing this plugin!
It only seems to work for posts filtered in one category. For me, that’s not so interesting.
I would be really interested to have articles sorted by post title:
> For ALL postings, (the …. /wp-admin/edit.php landing page)
> If I filter ther articles by author (I choose one author and display all his / her posts)
Is this possible? Thanks a lot!
Hello Bart,
Yeah, it’s possible, but it’s not what the plug-in is doing right now. We might add this functionality in a future version.
Good luck,
Gabriel
Thanks for your super-quick reply! Too bad…
hi gabriel
i am wondering if you might be able to advise as to how i might sort by title on the single post template level in wp 2.7.x? there appears to be no such parameter for the previous_post_link and next_post_link template tags but that is ‘where’ i would like to implement …
any suggestions would be VASTLY appreciated
thanks much.
– joyce lukaczer
The first way that comes to mind is editing the get_adjacent_post function. You can find it in wp-includes/link-template.php
Take a look at line 901. You can change “ORDER BY p.post_date” to “ORDER BY p.post_title”
Great plugin. Thank you !
Works great, thank you
You’re welcome. I’m glad it helps
This plugin would be a perfect fit for a project I am working on if it would sort not just category posts, but tag posts as well.
But your plugin is a big improvement on Alphabetical List, since you can choose a default order for all categories, and override that for certain ones.
Great suggestion. I’m adding it to the To-Do List. Thanks.
Excellent. Just what the doctor ordered.
Hi, great plug-in……..
although I was looking for something that would actually order the database table everytime a category received a new record,…. so specifically, the Next_link and Previous_link tags would be sorted….
I have a category of ‘artists’ and when I’m looking at the single post (single.php), I want the next/prev link at the top to show the next/previous in alphabetical order within the category.
Does your plug-in do this and am I missing how to do it? If not, please add this to the wish list (!) … also, so that the category list in the side-bar will work in alphabetical order… that is until I figure out how to write my own widget!
Meanwhile, many thanks for sharing your work
The next/prev sort order is hardwired into WordPress.
See wp-includes/link-template.php
Look at the line starting with “$sort =”
Is this plug-in working with 2.8? I’m using WP 2.84, PHP 5.
I’m trying to sort several categories alphabetically but no matter which way I try, ascending or decending, I end up with reverse alphabetcial, z-a
I also have several child categories, could that be the problem?
This plug-in is exactly what I need if it would work. Actually, it does work… just in reverse order. If necessary I’ll leave everything backwards and pretend I meant to do that. LOL
Thank you so much for the plug-in, I have searched and searched for a way to get posts to sort alphabetically and reverse is better than nothing by far.
The plug-in definitely works on our instance of WordPress 2.8.4, and incidentally we’re also running PHP 5, although it should also work perfectly with PHP 4.
The fact that you’re using child categories should not be a problem. I have no idea what the problem could be, I’ve never heard of this issue from anyone else. Are you sure you saved the new options and everything?
Yes, I’ve even cleared my cache several times. It’s strange. I have no clue what is going on but like I said, reverse alpha is better than nothing. Thank you for the plug-in. If I stumble upon the reason/solution for this reverse alpha thing, I’ll let you know.
You are a God for this. Sorry, I get excited when someone comes up for a simple, effective solution for my problems. Great plug-in, this is exactly what I needed.
((: thanx
Hi,
using the plugin on my site but I find one flaw…
I’m using it to sort posts in the content ‘part’.
In the sidebar I have a new query which pulls the last 5 posts.
But now these are ordered alphabeticly as well so it doens’t make any sense…
Is there a way to stop the sort on a certain point in the page ?
Sure. Just use this filter to switch to the wordpress default sorting:
function switch_to_default_sorting(){
return ‘post_date DESC’;
}
add_filter(‘posts_orderby’, ’switch_to_default_sorting’ );
Excellent, that does the trick…
but you might think about removing those rounded quotes (on this site)… i copied it and it gave me an error. took me a couple of minutes before I realised it was the quotes that did it.
Glad to be of service (:
PS any expectation when sorting for tags will be finished ??
I know how to create a new query (as workaround) but not how to seperate them into pages, so eagerly awaiting the updated version
@Beee:
It’s no big deal. Just use the WordPress conditional tags:
function custom_sorting(){
if ( is_tag(’some-tag-name’) ) return ‘post_title ASC’;
}
add_filter(’posts_orderby’, ’custom_sorting’ );
I am using WP2.8.4, I installed the plug-in, set it to sort by title ascending, but the posts remain unsorted. All posts are uncategorized. What am I missing?
Well, this plug-in only sorts posts in specific categories.
The posts should be sorted in the “uncategorized” category, and only there.
When I go to Posts>>Edit, and click on FILTER, the posts do get sorted in the “Uncategorized” category. However, it does not follow this sorting sequence in the displayed webpage.
I am using Thesis Openhook. Do I need to put something in any of the following: Before Post Box or Before Post? There is an option to execute PHP.
Just add this filter:
function custom_sorting( $orderby ){
if ( is_category(’your-category’) ) return ‘post_title ASC’;
else return $orderby;
}
add_filter(’posts_orderby’, ’custom_sorting’ );
Gabriel, I am new to WP. Where exactly do I add this filter?
You can either put in in your theme’s functions.php (i guess this would be the easiest) or make it into a plug-in.
Pay attention to the quotes, you will need to change them to standard PHP quotes. For some reason my theme always changes my quotation marks to that weird type.
OK, after adding it to my functions.php, what else do I have to do so that the “custom sorting” function gets executed when I add a new post?
For Bill:
I now see the misunderstanding.
The “custom sorting” function does not change the order of your posts in the database. It just sets the order for a MySQL SELECT query. If you added the filter in your functions.php, and it still doesn’t work, it must have something to do with the other plug-ins or with the theme itself. Some wierd interaction.
Something that you mentioned before comes to my mind:
“When I go to Posts>>Edit, and click on FILTER, the posts do get sorted in the Uncategorized category.”
This means that there must be a problem with your theme. Try using our plug-in with the default WP theme, and see if it works.
I think the problem is that my theme is not using the MySQL SELECT query, therefore the custom_sorting function is not getting executed. Where and how to add this query?
Wow! I have no idea. How does your theme read data from the database without using a SELECT query? Beats me. Seems impossible…
You’re absolutely right. My mistake. My theme MUST be using the SELECT query. Please forgive my ignorance and for wasting your time. I hope that someday I will be able to understand all this. Good luck to your career!
I just installed the plugin on Wordpress version 2.8.4 and when I change the sort order in the admin the posts don’t reflect the change on the site. Any ideas why?
Any help is appreciated.
Do they get sorted in the backend if you go to Posts>>Edit, and you filter by your category?
Yes under edit when filtered by category they show only the selected category.
Then please test to see if the plug-in works with the WordPress default theme.
Just checked it with the default theme with the same outcome.
http://www.cdmdigitalphoto.com/development/cms/
For ItWasMe:
Try deactivating all other plugins, and see if it works then. While at the same time using the defalut theme.
The plug-in works with a clean installation of WP 2.8.4
That’s for sure
I believe you which is why I am confused right now.
I have the default them on and all the plugins turned off except post sorting.
The first and third category is set to post title, ascending
The second category is set to post title, descending
Yet they all display in the same order.
http://www.cdmdigitalphoto.com/development/cms/
Quite weird… I have no idea what’s happening.
You can try and build the filter yourself. Something like:
function custom_sorting( $orderby ){
if ( is_category(’your-category’) ) return ‘post_title ASC’;
else return $orderby;
}
add_filter(’posts_orderby’, ’custom_sorting’ );
how and where would I add in this code? will I have to do it for every category?
and for the return are the options post_title DSC, post_title ASC, post_date DSC, post_date ASC
You could add it in your theme’s functions.php
If you want all your categories to be the same, you can just use is_category(), with no parameter.
And yes, those are the return options.
I’m a newbie to wordpress. I added the code to the functions.php file and nothing changes. Is there something else I need to add before/after the code you listed above?
It should have worked just like that.
Did you use any parameter for is_category() ?
I’m running into the same problem as ITWASME. The sorting option doesn’t seem to work under my wordpress 2.8.4.
Any idea why? if you are working on an upgrade do you know if/when it would be available?
I’m using wordpress as a CMS for my client and I know the lack of sorting options will bother him.
thanks for any help that you might have.
I can’t reproduce the error. It works on all our instances of WP 2.8.4
Try installing a new one, it will certainly work there,
Hi all,
I have the same issue as some above. The posts are sorted correctly within the admin listing of posts when limited by category, but remain sorted incorrectly on the actual site. I’m using the Minimalism theme but have tried the default and classic theme with no other luck.
I re-created the database awhile back on an earlier version of 2.8, so I don’t think it is an upgrade issue. I still am thinking it is a theme thing… so I’ll experiment more with this.
Any advice would be much appreciated.
This is getting weird. Please try it on a fresh install of WordPress… It’s like The Twilight Zone. There’s no way I can reproduce the problem…
Gabriel,
My apologies…. I had a navigation error and was looking at a previous install of wordpress / previous site. This works wonderfully.
Thank you and sorry for any confusion.
The plugin works great for the categories, but my homepage stays sortated on posts date. Is it my theme (Mandigo), or can it only sort categories?
Thank you in advance (and sorry for my bad English).
Yeah, it’s only for categories. Look into the posts_orderby hook. Sorting is no big deal.
Thank you for your quick reply
I’m trying to sort by date of event as oppose to date of post.
will this help?
Is “date of event” a custom field?
This plugin is just what I needed. have one category i want to change the posts sortorder for by date. I have posts in one category stacked in one page using a custom template I found on the WP site called ‘pageofposts’. But it seems your plugin does not work to change the sorting for this page/category. Do I need to add code to make this work? Would be simple enough to modify the template to change the sortorder but I’m not a programmer unfortunately.
Hello Jeroen,
If you want to use our plug-in, you need to use the regular category pages. If you use custom templates it will not work. Good luck with your project!
gabriel, are you going to add sorting by custom fields in the near future? if you will do it, you’ll become my biggest idol!
i have really big need to sort posts (in one category) alphabetically, but not by tittles.
regards
Unfortunately it’s not really a top priority for us right now…
seems to be incompatible with WP 2.8.6– unless it is conflicting with another of my plugs. i get a blank screen when i browse to your control panel. thanks!
I’ve just tested it on a 2.8.6 instance, and it’s working properly…
The plugin works great … but it is not sorting the posts on the front page… how do we do that?
You can just add your own custom filter to the posts_orderby hook (take a look here) and use the conditioal tag is_home() and return “post_title ASC”.
can you be more specific — am i supposed to add this custom filter to your plug-in’s hook, or the wordpress stylesheet? what exactly will the code look like and where should it be placed in context? thanks!
no, it has nothing to do with our plugin. and also, it has nothing to do with your stylesheet. you need to put the code either in your theme’s functions.php or in a custom plugin.
the code goes like this:
function custom_sorting( $orderby ){
if ( is_home() ) return ‘post_title ASC’;
else return $orderby;
}
add_filter(’posts_orderby’, ’custom_sorting’ );
Hi, Howcan i activate the plugin?
i have installedit activate it succesfully on my wp admin console but, i dont know how to activate the admin console and the config files, they show (inactive)
Thanks
I’m not sure I understand what you’re talking about. Can you see the “Post Sorting” administration menu, under “Posts” ?
hi dear, I have downloaded your plugin to use my little project of my native and endangered language (berber) in libya, anyway, its written using arabic alphabet . your plugin works like a charm for the individual categories , what i need it to do is to list in the post page alphabetically for all categories together.
are you aware or is it possible to use that with your plugin.
Hey! Unfortunately, the current version of this plugin does not include the feature you mention…
Good luck with your project!
16 Trackbacks
[...] WP Post Sorting [...]
[...] WP Post Sorting [...]
[...] WP Post Sorting [...]
[...] WP Post Sorting [...]
[...] Gabriel made his first plugin for Wordpress, WP Post Sorting. This plugin allows sorting of posts by Post Title (ascending or descending) or Post Date [...]
[...] WP Post Sorting [...]
[...] WP Post Sorting [...]
[...] WP Post Sorting [...]
[...] WP Post Sorting [...]
[...] WP Post Sorting [...]
[...] WP Post Sorting [...]
[...] WP Post Sorting [...]
[...] “WP Post Sorting” Plugin | Reflection Media This plugin allows sorting of posts by Post Title (ascending or descending) or Post Date (ascending or descending), customizable for each category. (tags: wp plugins sort) [...]
[...] traducerea wordpress în limba română pentru versiunea curentă și fac o treabă bună, au creat WP Post Sorting Plugin și un plugin care “vorbește” cu tine, Talky [...]
[...] WP Post Sorting: a plugin to list entries in alphabetical order on letter listing pages [...]
[...] WP Post Sorting – sort posts differently by category – http://www.reflectionmedia.ro/2008/12/wp-post-sorting-plugin/ Possibly related posts: (automatically [...]