Posted By

hussong on 04/12/09


php sort template wordpress order custom

Versions (?)

Who likes this?

10 people have marked this snippet as a favorite


Wordpress: Sort Order by Custom Field for specific Category

 / Published in: PHP

Put this before the loop in archive.php to have posts in the archive for category '26' sorted by custom field 'date'. Assign sortable values like '090501' or '2009-05-01' to those fields when editing posts and your Upcoming Events will be sorted by the future date at which they will take place.

  1. <?php if (is_category('26')) { $posts = query_posts($query_string. '&orderby=meta_value&meta_key=date&order=DESC' ); } ?>

Report this snippet  


RSS Icon Subscribe to comments
Posted By: ninjamuk on October 8, 2009

You just saved the day :) Amazing snippet.

Posted By: ScottHack on October 28, 2009

Would this also work if I assigned a dollar value to a custom field? Say I wanted to sort the posts from highest to lowest for a site that had a site that sold screw drivers.

If I used a custom field of price and assigned a value of $7.00 , $8.00 , $10,000 , etc -- would the same principle work? Could someone assist me with that code?

Posted By: techjacker on August 24, 2010

You are the MAN!

Posted By: hussong on September 19, 2010

Thanks everyone and sorry for replying so late, I didn't expect anyone to view this little snippet, let alone comment on it...

Posted By: charlie2 on September 27, 2010

Hello! Is it any way to change this code so it will show the post even if date-field is empty? I would be happy if I could get them to appear last but i cant see them at all...

Posted By: hussong on January 28, 2011

To get a custom sort order for the on the previous/next post links, see:

You need to login to post a comment.