Using dynamic conditions in ‘posts_where’ filter

I was afraid you wanted that one. You can’t really do that. Follow the link for some workarounds. You could also set a variable or a constant in functions.php, or create a theme option for this. Then use that in your function.

function smbd_cats_by_days ($where="") {
    // global $days_limit; // if a variable
    // $days_limit = DAYS_LIMIT; // if a constant
    // $days_limit = get_option('days_limit',100);
    // you have to uncomment one of the above, 
    // depending on your choice of mechanisms
    $where .= " AND post_date < '" . date('y-m-d', strtotime("-{$days_limit} days")) . "'";
    return $where;
}

add_filter('posts_where', 'smbd_cats_by_days');

http://codex.wordpress.org/Function_Reference/get_option

Leave a Comment