Skip to content Skip to tags Skip to twitter news Skip to blog roll Skip to categories Skip to archives Skip to recent posts

May 4, 2008

WordPress: Getting all post between two dates

by Benjamin Sterling in Tips, Wordpress on May 4, 2008 @ 1:00 pm

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:

PHP:
  1. $querystr = "
  2.     SELECT *
  3.     FROM $wpdb->posts as wpost
  4.     INNER JOIN $wpdb->term_relationships
  5.     ON (wpost.ID = $wpdb->term_relationships.object_id)
  6.     INNER JOIN $wpdb->term_taxonomy
  7.     ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
  8.     AND $wpdb->term_taxonomy.taxonomy = 'category'
  9.     AND $wpdb->term_taxonomy.term_id IN (3)
  10.     where wpost.post_date BETWEEN SUBDATE(CURDATE(), INTERVAL 1 YEAR) and ADDDATE(CURDATE(), INTERVAL 1 DAY) ORDER BY wpost.post_date DESC
  11. ";
  12. $pageposts = $wpdb->get_results($querystr, OBJECT);

Tags: ,

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

RSS Feed Link

42 queries. 0.426 seconds. Powered by WordPress visitor stats