Novice@PHP Posted October 2, 2010 Share Posted October 2, 2010 I have made a bit of code that queries the database for articles published within the last year. <?php echo $numposts = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' AND 'post_date' > '" . date("Y") . "-01-01-01 00:00:00'"); if (0 < $numposts) $numposts = number_format($numposts); ?> I really don't even know how it works and need to because I want to figure out how to make it also get articles published within the last day, month and week. I suppose if I understood what needs changing I might be able to work with it but so far everything I've tried either returns the exact same number or nothing at all. It's for Wordpress just to note. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/214977-i-dont-understand-how-this-code-works-advice/ Share on other sites More sharing options...
jskywalker Posted October 2, 2010 Share Posted October 2, 2010 for post this year you have: <?php echo $numposts = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' AND 'post_date' > '" . date("Y") . "-01-01-01 00:00:00'"); if (0 < $numposts) $numposts = number_format($numposts); ?> for posts this month you could do: <?php echo $numposts = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' AND 'post_date' > '" . date("Y") . "-01-01-01 00:00:00' AND `post_date`>'2010-'.date("m").'-01-01 00:00:00'"); if (0 < $numposts) $numposts = number_format($numposts); ?> Quote Link to comment https://forums.phpfreaks.com/topic/214977-i-dont-understand-how-this-code-works-advice/#findComment-1118283 Share on other sites More sharing options...
jskywalker Posted October 2, 2010 Share Posted October 2, 2010 sorry it look this crappy..... But most it is simple SQL, beside some date-formatting in PHP Aboud those data-formats, you can check http://www.php.net/date and everything about MySQL is here: http://dev.mysql.com/doc/refman/5.1/en/index.html So, which part was not clear to you ? :'( ;) Quote Link to comment https://forums.phpfreaks.com/topic/214977-i-dont-understand-how-this-code-works-advice/#findComment-1118284 Share on other sites More sharing options...
Novice@PHP Posted October 2, 2010 Author Share Posted October 2, 2010 I've actually just discovered the whole line of code doesn't even work in the first place. All it is doing is grabbing the total number of posts. Not the year. Quote Link to comment https://forums.phpfreaks.com/topic/214977-i-dont-understand-how-this-code-works-advice/#findComment-1118290 Share on other sites More sharing options...
jskywalker Posted October 2, 2010 Share Posted October 2, 2010 possible, because in the SQL-statement is: 'post_date' which should read: `post_date` The first one (between '') is a string, the second one (with the back-quotes), is a reference to the field named post_date.... and 'post_date' is always > '2010-10-02.....'.. Quote Link to comment https://forums.phpfreaks.com/topic/214977-i-dont-understand-how-this-code-works-advice/#findComment-1118291 Share on other sites More sharing options...
Novice@PHP Posted October 2, 2010 Author Share Posted October 2, 2010 possible, because in the SQL-statement is: 'post_date' which should read: `post_date` The first one (between '') is a string, the second one (with the back-quotes), is a reference to the field named post_date.... and 'post_date' is always > '2010-10-02.....'.. Nice thanks for that. I did figure that out eventually. This works perfectly too but (cont below) <?php echo $numposts = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' AND `post_date` > ' 2010-01-01 13:24:53'"); if (0 < $numposts) $numposts = number_format($numposts); ?> If I try to use the date("Y") or date("m") like so ' ".date("Y") . "-01-01 13:24:53'"); or ' 2010-".date("m") . "-01 13:24:53'"); It doesn't work. I guess maybe I misunderstand how to add ".date("m") . " in to make sure it is added correctly and ths it won't work unless I do. If I'm not making sense shout at me hehe. Thanks dude. Quote Link to comment https://forums.phpfreaks.com/topic/214977-i-dont-understand-how-this-code-works-advice/#findComment-1118293 Share on other sites More sharing options...
Novice@PHP Posted October 2, 2010 Author Share Posted October 2, 2010 Actually correction. I got it. And now believe I understand how to query a database. First time for everything. Thanks dude well appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/214977-i-dont-understand-how-this-code-works-advice/#findComment-1118295 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.