Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have the following code which gives be posts that are published in the last 100 days

function smbd_cats_by_days ($where = '') {

    $where .= " AND post_date < '" . date('y-m-d', strtotime("-100 days")) . "'";
    return $where;
}

add_filter('posts_where', 'smbd_cats_by_days');

It is working fine. But now I want to make this function generic. (ie) I want the number of days to be stored in a variable instead of hard coding it as 100.

How to do that?

share|improve this question
1  
How do you want to set the variable? Not technically, programmer-y "how", but human "how"? Is this a theme option? Do you want to pass a parameter when you add the filter? Set a constant? What? – s_ha_dum Dec 8 '12 at 16:40
I want to pass it as a parameter when I add the filter. – Sudar Dec 8 '12 at 16:41

1 Answer

up vote 1 down vote accepted

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

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.