WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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,
share|improve this question
Close-voted as off-topic. This question appears to be a SQL syntax question, rather than a WordPress question. – Chip Bennett May 1 '12 at 15:38
the appropriate answer to this involves WordPress knowledge, aka use a WP_Query object – Tom J Nowell Feb 1 '13 at 17:30

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'
share|improve this answer

Try this:

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

instead of:

AND year($wpdb->posts.post_date) = 2010,
share|improve this answer
Thanks for replying. Unfortunately this didn't work. :( Any other ideas? – Dan Lee Dec 9 '11 at 12:56
Does $wpdb->posts.post_date need to be passed through strtotime() first, perchance? – Chip Bennett Feb 10 '12 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'
share|improve this answer

Your Answer


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.