Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
add comment

3 Answers

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'
    )
);
share|improve this answer
add comment

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
add comment

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
add comment

Your Answer

 
discard

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.