A really simple and lightweight plugin (73 lines of code – comments included) to sort posts on-the-fly without making a new query to the database (improves performance).
- Supports all
ordervalues of the query_posts function except
meta_value_num(which require a database query).
- Supports changing the order of all types of posts, including custom post type posts and custom post type “archive” posts.
This plugin adds the function
sort_query_posts_by(string $order_by [, string $order]) to the global context.
$order parameter is optional. Its default value is
asc (ascending order).
Call this function before the loop to change how posts are ordered.
After calling this function you can show the posts as you normally would.
You can sort posts by:
This is specially useful in two cases:
- When you need to reorder the posts returned by the query that WordPress creates from your given URL. Custom post type “archive” posts are a great example of this case.
- When you need the posts returned by your customized query (e.g.
query_posts()) to be shown more than once on the same page and ordered differently.
<?php sort_query_posts_by('title', 'desc'); ?>
The example above will sort posts by their title in descending order without making a new query to the database.
This way sorting is performance friendly.
<?php sort_query_posts_by('ID'); ?>
The example above will sort posts by their ID in ascending order.
<?php sort_query_posts_by('rand'); ?>
The example above will sort posts randomly. When sorting randomly
$order is ignored.
Plugin developed by Túbal Martín at www.margenn.com.
sort-query-postsfolder to the
- Activate the plugin through the ‘Plugins’ menu in WordPress
- Use the added function
sort_query_posts_byafter any wordpress query and your posts will be sorted to your needs.
Ez dago berrikuspenik plugin honentzat.
Laguntzaileak eta Garatzaileak
“Sort Query Posts” software librea da. Ondoko pertsonek egin dizkiote ekarpenak plugin honi.Laguntzaileak
- Code refactored for improved performance. Updating is recommended.