Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm working on a job board-type site, and the client want to sell featured posts. They want the featured posts to expire from showing up in the "featured" section after a predetermined period of time, but continue to show up in the rest of the site.

My solution to this problem would be to assign a custom field to posts in the "featured" category stating the number of days it's to be featured for, say, 15. So the logic of the query would be something like: IF the post is category "featured" AND the custom field "featuredexpiredays" IS LESS THAN days since publication, get the post.

I have a script for calculating the number of days since publication, I'm just failing completely at figuring out how to glom all this together into a single query.

Do you have any suggestions on how to do this, or alternately, suggestions of a different way make my site behave in this way? Thanks so much!

share|improve this question
1  
Don't forget to mark one of these responses as accepted and maybe even upvote some answers if you feel they have given you good feedback :) –  Steven Aug 19 '11 at 7:18

2 Answers 2

It's maybe easier to set an expiration date rather than period, then you can do a meta query for posts with expiration dates greater than today:

$args = array(
            'posts_per_page' => -1,
            'meta_key' => 'expiration_date',
            'meta_value' => date('Y-m-d'),
            'meta_compare' => '>=',
            'orderby' => 'meta_value',
            'order' => 'ASC'            
);
$posts = new WP_Query($args);
share|improve this answer
1  
Should probably use the meta_query parameter for this instead of meta_key, meta_value, and meta_compare, right? –  tollmanz Aug 18 '11 at 21:30

I found this: http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query
I have modified the date to retrieve the last 15 days. It's untested, but should work.

$expireDays = '15';

$querystr = "
    SELECT $wpdb->posts.* 
    FROM $wpdb->posts, $wpdb->postmeta
    WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id 
    AND $wpdb->posts.post_status = 'publish' 
    AND $wpdb->posts.post_type = 'post'
    AND $wpdb->posts.post_date >= ( CURDATE() - INTERVAL $expireDays DAY )
    ORDER BY $wpdb->posts.post_date DESC
 ";

$pageposts = $wpdb->get_results($querystr, OBJECT);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.