Jump to content

I don't understand how this code works - Advice?


Novice@PHP

Recommended Posts

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?

Link to comment
Share on other sites

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); ?>

 

Link to comment
Share on other sites

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.....'..

 

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.