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 want to get posts from the last 30days in a sidebar but I don't want to use query_posts, is there a way to achieve this with the get posts function?

share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

Yes, simple add a filter before you call it and remove it after you do

function filter_where_wpa89154($where = '') {
    //posts in the last 30 days
    $where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
    return $where;

add_filter('posts_where', 'filter_where_wpa89154');
$args = array(
    'posts_per_page'  => 5,
    'post_type'       => 'post',
    'post_status'     => 'publish',
    'suppress_filters' => false
$posts = get_posts($args);
remove_filter('posts_where', 'filter_where_wpa89154');

notice the 'suppress_filters' => false which is what makes this happen with get_posts

share|improve this answer
How would I add this to a loop exactly? I'm a beginner. –  Chozen Mar 3 '13 at 15:30
humm that's a whole other question :) in that case i would suggest you use a plugin like wordpress.org/extend/plugins/flexible-posts-widget or read up on WP-Query and Widgets_API –  Bainternet Mar 3 '13 at 18:25
I actually got it to work. Thanks –  Chozen Mar 3 '13 at 21:15
add comment

Your Answer


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.