Plugin Directory

Post Sorting Reloaded

Allows sorting of posts by title, comment count, and other criteria for various pages, including category pages, tag pages, home page, etc.

Can I sort by custom fields?

Yes and no. In order to sort by custom fields, you also need to use the posts_join filter in addition to the posts_orderby filter.

For the posts_orderby filter you can use the plugin, but for the posts_join filter, you're going to have to write the code yourself.

Just add the following code either in your theme's functions.php or in a new little plugin:

add_filter('posts_join', 'my_posts_join');

function my_posts_join($join){
    global $wpdb;
    $join .= " LEFT JOIN $wpdb->postmeta wpmeta ON ($wpdb->posts.ID = wpmeta.post_id AND wpmeta.meta_key = 'price')";
    return $join;
}

Replace 'price' with whatever your custom field is called.

Now you have access to your custom field by using wpmeta.meta_value as your Sort Type.

Of course, if you need the numerical value of your custom field, you can use something like ABS(wpmeta.meta_value).

Requires: 3.0.1 or higher
Compatible up to: 3.3.2
Last Updated: 2011-8-31
Downloads: 7,106

Ratings

4 stars
4.4 out of 5 stars

Support

Got something to say? Need help?

Compatibility

+
=
Not enough data

0 people say it works.
0 people say it's broken.

100,2,2 100,4,4 100,1,1 100,1,1
100,1,1
100,1,1
100,3,3
100,1,1
100,1,1
100,4,4
100,1,1
100,1,1