WordPress: Getting all post between two dates
Today I had a need to get all posts between to dates in WordPress, between now and a year ago. After searching and searching I could find the answer I needed. So I decided to just write the query myself which I have done many times before but not with WordPress’ database scheme.
Below is the code that I used, basically I need to grab all posts from a particular category, in this case category 3, and I also needed the posts to be between today’s date and this date last year. Some help came from this post here.
Well, here it is:
- $querystr = "
- SELECT *
- FROM $wpdb->posts as wpost
- INNER JOIN $wpdb->term_relationships
- ON (wpost.ID = $wpdb->term_relationships.object_id)
- INNER JOIN $wpdb->term_taxonomy
- ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
- AND $wpdb->term_taxonomy.taxonomy = 'category'
- AND $wpdb->term_taxonomy.term_id IN (3)
- where wpost.post_date BETWEEN SUBDATE(CURDATE(), INTERVAL 1 YEAR) and ADDDATE(CURDATE(), INTERVAL 1 DAY) ORDER BY wpost.post_date DESC
- ";
- $pageposts = $wpdb->get_results($querystr, OBJECT);


