Please can you guys help me check if my syntax is correct. I don’t get any errors using this code but I just wanted few opinions on whether I’m doing it right or not.
I’m using a multi-site WordPress installation and I’m checking to see if the featured blog is older than a day but less than a week old:
/* Work out blog of the day */
$featured_id = get_option('featured_blog'); # Current ID
$featured_time = get_option('featured_time'); # Current Time
if($featured_time > time()-86400 && $featured_time < time()-604800) // more than 1 Day ago but less than a week
{
$query = "SELECT blog_id FROM `wp_blogs` WHERE public = '1' and archived = '0' and spam = '0' and deleted = '0' AND blog_id NOT IN ('".$featured_id."', '1','27') ORDER BY rand() limit 1";
$featured_id = $wpdb->get_var($query);
update_option('featured_blog', $featured_id);
update_option('featured_time', time());
}
According to http://codex.wordpress.org/Database_Description#Table:_wp_blogs, the column blog_id is int, so you should not enclose the values in quotes
Same for columns
public,spamanddeleted.Besides that, which isn’t a real problem, I can’t spot any syntax error.
But contrary to your title, you don’t use the time in your query.
To the
if()statement, this will never become true, because$featured_timecannot be> now - 86400and< now-604800at the same time. If you want the condition as in your comment, sayOn a
time()-line this looks more or less like thisSo, if
$featured_timeshould be between a week ago and a day ago, it must be greater than a week ago (> now – 604800) and less than a day ago (< now – 86400).