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.

Okay, I'm trying to get posts that have been published within the last x amount of time, options being 8, 24 or 72 hours. I have the variables being passed to my featureHandler.php via ajax, then returning the posts, here is how I'm doing it:

$vars = $_GET['vars'];
$time = $vars[0];
$order = $vars[1];

if ($time == 8 ) {
    function filter_where($where = '') {

        //posts in the last 8 Hours
        $where .= " AND post_date > '".date('Y-m-d H:i:s', strtotime('-8 hours'))."'";
        return $where;
    }
    add_filter('posts_where', 'filter_where', 10, $time);
} elseif ($time == 24) {
    function filter_where($where = '') {

        //posts in the last 24 Hours
        $where .= " AND post_date > '".date('Y-m-d H:i:s', strtotime('-24 hours'))."'";
        return $where;
    }
    add_filter('posts_where', 'filter_where', 10, $time);
} elseif ($time == 72) {
    function filter_where($where = '') {

        //posts in the last 72 Hours
        $where .= " AND post_date > '".date('Y-m-d H:i:s', strtotime('-72 hours'))."'";
        return $where;
    }
    add_filter('posts_where', 'filter_where', 10, $time);
}

?>

<div class="article-wrapper">
<?php

query_posts(array(
       'post__not_in' => get_option('sticky_posts'), 
));

$itemCount = 0;

while ( have_posts() ) : the_post();

This is semi working, I think, except that a post I just created, only shows up in 24 or 72, but not in the case that $time==8, why is that? And how can I make this code more efficient?

Thank you SINCERELY.

share|improve this question
add comment

1 Answer 1

up vote 0 down vote accepted

here is a shorter version that assumes the GET parameter ?time=[integer] (for example ?time=8)

add_filter('posts_where', 'filter_where', 11);
function filter_where($where = '') {
    if(isset($_GET['time'])){
        $time = $_GET['time'];
        $time=(int)$time; // only allow integers
        if(in_array($time,array(8,24,72))){
            $where .= " AND post_date > '".date('Y-m-d H:i:s', strtotime('-".$time." hours'))."'";
        }
    }
    return $where;
}
share|improve this answer
    
Do I need to remove the filter after the query? –  Joshua Richards Feb 7 '13 at 2:30
    
Rather, do I need to remove the filter, prior to updating the filter and running the query again? –  Joshua Richards Feb 7 '13 at 2:30
    
usually I put my filters in the file functions.php –  birgire Feb 7 '13 at 2:34
    
any luck so far? –  birgire Feb 8 '13 at 12:46
    
Well the variable in the $where . = etc, was not reading correctly, so instead i used a variable to $fromTime = '-' . $time . 'hours; and put $fromTime directly into strtotime. This worked, and i found out the reason why the correct posts were not displaying was because the date function returns UTC, and the post times are saved in local time. –  Joshua Richards Feb 8 '13 at 15:10
add comment

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.