I have a query were I'm grabbing all post types of "news" in 2010, but it doesn't select the posts from that year. What am I doing wrong?

$querystr = "
    SELECT $wpdb->posts.*
    FROM $wpdb->posts, $wpdb->postmeta
    WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
    AND $wpdb->posts.post_status = 'publish'
    AND $wpdb->posts.post_type = 'news'
    AND year($wpdb->posts.post_date) = 2010,
    ORDER BY $wpdb->posts.post_date DESC

Taking this line out does fetch the news posts, but really I want them from a particular year.

AND year($wpdb->posts.post_date) = 2010,
  • Close-voted as off-topic. This question appears to be a SQL syntax question, rather than a WordPress question. Commented May 1, 2012 at 15:38
  • the appropriate answer to this involves WordPress knowledge, aka use a WP_Query object
    – Tom J Nowell
    Commented Feb 1, 2013 at 17:30

3 Answers 3


Don't use the direct SQL query, there's no point and it's bad practice.

Instead use WP_Query:

$year = date('Y');
$query = new WP_Query(
        'year' => $year,
        'post_type' => 'news'

Try this:

AND year($wpdb->posts.post_date)='2010',

instead of:

AND year($wpdb->posts.post_date) = 2010,
  • Thanks for replying. Unfortunately this didn't work. :( Any other ideas?
    – Dan Lee
    Commented Dec 9, 2011 at 12:56
  • Does $wpdb->posts.post_date need to be passed through strtotime() first, perchance? Commented Feb 10, 2012 at 13:34

Try this instead:

AND $wpdb->posts.post_date BETWEEN '2010-01-01 00:00:00' AND '2011-01-01 00:00:00'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.