Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to get all published posts titles by author id 2 most recent by post date for the last days. Here is my query:

"SELECT post_title FROM $wpdb->posts WHERE post_status = 'publish' AND post_author = 2 ORDER BY post_date DESC LIMIT 3 "

This displays most recent 3 posts instead posts of last 3 days. How to get that query right?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

You are asking for the last three posts ordered by post date, not for the posts from the last three days-- ORDER BY post_date DESC LIMIT 3. post_date has a time component to it. It is not just marked with a date. Even so, that LIMIT would restrict the query to last three in the list, not all of the posts that are more recent than three days.

What you need is something like this:

SELECT post_title 
FROM $wpdb->posts 
WHERE post_status = 'publish' 
AND post_author = 2 
AND post_date > DATE_SUB(CURDATE(), INTERVAL 3 day)
ORDER BY post_date DESC

See the following for other options and caveats: http://wordpress.stackexchange.com/a/96562/21376

share|improve this answer
    
Thanks, yeah I was taking wrong approach. –  10wtaylor Apr 21 '13 at 17:06

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.