Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

order posts by custom field

Tags:

wordpress

i'm using this query

$numposts = get_option('posts_per_page');
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts('cat=5&showposts='.$numposts.'&paged=' . $paged); 

how to order the posts by custom field?

like image 495
eyalb Avatar asked Nov 02 '10 08:11

eyalb


1 Answers

how about?

$customfield = "MyCustomFieldName";
$numposts = get_option('posts_per_page');
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts('cat=5&showposts='.$numposts.'&paged='.$paged.'&meta_key='.$customfield.'&
orderby=meta_value&order=DESC');

or instead of creating a new variable just to hold the custom field just add it straight into the query..

query_posts('cat=5&showposts='.$numposts.'&paged='.$paged.'&meta_key=mycustomfield&
orderby=meta_value&order=DESC')
like image 180
Marty Avatar answered Nov 15 '22 06:11

Marty