tags:

views:

99

answers:

2

I'm trying to show expired posts AFTER non-expired posts. How can I do this?

I've added a custom field "Expiration Date", in which I store the expiration dates in yyyy/mm/dd format. Problem is, if I order my results by this field, future expiry dates come first.

So I created a repeating cron-job which compares the dates and creates a secondary custom field "Expiration Date Passed" for posts whose dates have passed. I tried ordering by this field, but WP only shows posts with a value for this field - IE posts with no expiry date, or expiry dates in the future, don't show. So I tried auto-adding values '99999999' for any post which haven't expired yet. Problem is, WP can't order by custom field values THEN date- IE the first posts with value '99999999' are in a random order.

I also tried doing two queries for posts, one without expired posts, one with, then merging these two arrays. So the data is in the right order - but it screwed up WP's pagination.

Help, I'm running out of ideas!

A: 

Since you have an "Expiration Date Passed" custom field , you could first create two sets of Posts using that custom field in your get_posts arguments to differentiate between current & expired Posts

$meta_key and $meta_value (string) (optional) Only show posts that contain a meta (custom) field with this key and value. Both parameters must be defined, or neither will work. Default: None

extract from: http://codex.wordpress.org/Function_Reference/wp_get_recent_posts

then you'll be able to sort each set the way you want

PatrickS
A: 

That might work, but I am trying to sort the posts on my category pages. Wp_get_recent_posts function is usually used for creating custom loops, not modifying 'the loop' in category (archive template) pages.

In the end I sorted it with this. I added this code to the top of my archive template:

global $query_string;
query_posts($query_string . "&orderby=meta_value&meta_key=Expiration Date Passed&order=DESC");  

I created a "sort" custom field called "Expiration Date Passed". A cron job then looks to see whether the post has an expiration date. If it doesn't, or if the date is in the future, it puts the post's publish date + 20 years in the sort column. If the post's expiration has passed, it puts the post's publish date in the sort column. Thus it results in the order I was after:

1) Posts which haven't expired, in date order 2) Posts which have expired, in date order

Thought I would post that solution in case anyone else wanted to know.

Joe Smalley