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 am working on the following code which should display the most popular posts that are X days old ($days = 7;)

(1) The main problem here is with my filter_where function. I want to pass the number of days from outside the function but it wont filter the results. The only way it filters them is to insert the variable inside the function. So, how can I use my variable from outsite the function?

(2) Another question I have is, In my get_topposts function, what happens after I return the query? Will the remove_filter and wp_reset_postdata still get executed or not? If not, how can I do so?

function get_topposts($days) {
//$ago = 7; // days
//$timestamp = time() - ($ago * 86400);

    function filter_where( $where = '' ) {
                   $where .= " AND post_date > '" . date('Y-m-d', strtotime('-'.$days.' days')) . "'";
                // $where .= " AND post_date > '" . date('Y-m-d', $timestamp) . "'";
                    return $where;
    }
    add_filter( 'posts_where', 'filter_where' );

     //$args = array(); 

    $the_query = new WP_Query( $args );
    return $the_query;
    remove_filter( 'posts_where', 'filter_where' );
    wp_reset_postdata();
}

EDITED: This is how it looks now after applying comments made by Milo

function filter_where( $where = '' ) {
//$days = apply_filters('custom_where_clause', '-30days');
//if(!$time)
            //$days = '-30days';
            // posts in the last 30 days
             //  $where .= " AND post_date > '" . date('Y-m-d', strtotime('-'.$days.' days')) . "'";
              $where .= " AND post_date > '" . date('Y-m-d', strtotime('-7days')) . "'";
                return $where;
}
//add_filter('custom_where_clause', 'get_topposts');

function get_topposts($posts) {
$the_query = get_transient('get_topposts');
if (empty($the_query)) {
    //$ago = 7; // days
    //$timestamp = time() - ($ago * 86400);
    //$days = 7;

    add_filter( 'posts_where', 'filter_where' );
    $args = array(
        'posts_per_page' => $posts,
        //'category_name' =>  'NAME OF CATEGORY',
        'post_status'=>'publish',
        'meta_key' => 'postviews',
        'orderby' => 'meta_value_num',
        'order' => 'DESC',
    ); 
    // The Query

    $the_query = new WP_Query( $args );
    set_transient('get_topposts', $the_query, 60*60*2);
    remove_filter( 'posts_where', 'filter_where' );
}
return $the_query;
}
share|improve this question
1  
this question has two answers for exactly this case. a couple of comments- don't nest functions in php, it's bad form. nothing after return gets executed, put that line last. wp_reset_postdata is unnecessary here, since you haven't yet touched the global $post. –  Milo Jul 30 '13 at 0:36
    
Thank you for your comments. I got rid of the nested functions, removed wp_reset_postdata, and moved return to the end. Your solution on the question you mentioned looks very neat; however, I am looking for a simpler solution (if possible) that I can understand a little bit better. –  Gixty Jul 30 '13 at 19:56
    
Perhaps you could Edit your question with your code changes. –  Charles Clarkson Jul 31 '13 at 6:42
    
ok, I just edited it to reflect the new changes. –  Gixty Aug 1 '13 at 13:54
add comment

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.