eyalb
eyalb

Reputation: 3022

order posts by custom field

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?

Upvotes: 7

Views: 2619

Answers (2)

alekone
alekone

Reputation: 127

I used a plugin for that: http://www.dyasonhat.com/plugins/wp-smart-sort/

Upvotes: 0

Marty
Marty

Reputation: 4657

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')

Upvotes: 8

Related Questions