There may be a simpler way, but I am certain that it can be done using the technique shown in this code.
You will need to register three filters:
add_filter('posts_fields','mam_posts_fields');
add_filter('posts_join','mam_posts_join');
add_filter('posts_orderby','mam_posts_orderby');
$paged = (intval(get_query_var('paged'))) ? intval(get_query_var('paged')) : 1;
Then, set up the values for the filters to add to the query:
$mam_global_fields = 'intval(wpmeta.meta_value) as sortval';
$mam_global_join = "LEFT JOIN $wpdb->postmeta wpmeta ON
({$wpdb->posts}.ID = wpmeta.post_id AND wpmeta.meta_key = 'priority')";
$mam_global_orderby = 'sortval';
query_posts("paged=$paged")
And, either in functions.php or at the end of your template, define the filters:
<?php function mam_posts_fields ($fields) {
global $mam_global_fields;
return "$fields, $mam_global_fields";
}
function mam_posts_join ($join) {
global $mam_global_join;
return "$join $mam_global_join";
}
function mam_posts_orderby ($orderby) {
global $mam_global_orderby;
return "$mam_global_orderby, $orderby";
}?>
Hi vtxyzzy,
Thank you for that. But it’s way over my head and it appears to be doing much more than I need. I already have the orderby set up – I just need it to recognize double digits. Is that what all of that above is doing?
thanks again
If WordPress is storing those custom field values as strings (which is probably is), then ’10’ would come before ‘2’ in a sort. What I would do is start numbering at ‘101’ instead of ‘1’. That way, the numbers will sort in the correct sequence.
Hi ambrosite,
That did not appear to make any difference actually.
I took a story I wanted to be first and used “101” – it did show up first. I then changed the priority to “110” (to simulate “10”) and it still remained on top of the other stories with smaller priority numbers. Did I misunderstand your suggestion?
Thanks!
Scratch that – I think it’s working after all – I went back in and changed all the priorities to use the 3 digits and it now appears to be working. Thanks!!!
Pad your numbers with zeros and it will work just the same..
eg..
001
002
011
100
..etc..
Cool – thanks! That seems to be working fine. π
Apologies for jumping in on this old thread but I’m trying to do the same thing as the OP, i.e., sort posts according to a priority set as a custom field, but where exactly to you put the query, “query_posts(‘meta_key=priority&orderby=meta_value&order=ASC’);” to sort the posts wherever they appear, e.g., home page, archive page, recent posts widget, etc?
There’s an old plugin http://ww.wp.xz.cn/extend/plugins/wp-smart-sort/ that let you sort by custom field but it doesn’t work in the current version of WP, and another plugin http://ww.wp.xz.cn/extend/plugins/post-sorting-reloaded/ will let you sort on any field in the wp-posts table but custom fields are not in the table although it does work if you put the priority number in an existing field like excerpt.
Ideally I would like to sort by priority and then date if it doesn’t have a priority. Any suggestions?
Thanks in advance.