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 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:

  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);
back to beginning of this post back to skip to links
Tags: ,

If you liked this article why don't you share it:

Stumble it delicious Digg it Reddit it DZone it Bump it Mixx it! Buzz up! E-mail

Comments are closed.

Learn from my mistakes, I got burnt by the flame, you don't have to.

RSS Feed Link My Hosting of Choice

70 queries. 0.273 seconds. Powered by WordPress visitor stats