0

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,
2
  • 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
1

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(
    array(
        'year' => $year,
        'post_type' => 'news'
    )
);
0

Try this:

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

instead of:

AND year($wpdb->posts.post_date) = 2010,
2
  • 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
0

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, privacy policy and cookie policy

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