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 posts that use a custom field for start date and end date. query_posts returns an array of posts that exist in the category I'm filtering. How do I query posts using this custom field that has date i.e. 03/11/2010 and not the full array. Pagination works on the full array so it returns all posts. I can use an if else to only show the posts newer that today, then pagination doesn't work. Would I have to build a custom mysql query?

share|improve this question
add comment

migrated from stackoverflow.com Mar 26 '11 at 9:18

This question came from our site for professional and enthusiast programmers.

3 Answers

<?php

 $querystr = "
    SELECT wposts.* 
    FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
    WHERE wposts.ID = wpostmeta.post_id 
    AND wpostmeta.meta_key = 'date_field' 
    AND wpostmeta.meta_value = '03/11/2010' 
    AND wposts.post_status = 'publish' 
    AND wposts.post_type = 'post' 
    ORDER BY wposts.post_date DESC
 ";

 $pageposts = $wpdb->get_results($querystr, OBJECT);

 ?>

Then you would access $pageposts as an object

share|improve this answer
    
this I believe is my answer! interested in doing some WPMU development? –  InnateDev Mar 16 '10 at 12:29
    
I would appreciate you checking this off as an answer! –  st4ck0v3rfl0w Mar 17 '10 at 20:11
    
I hunted for that large 'tick'... thanks for the great answer. –  InnateDev Mar 18 '10 at 14:59
add comment

You can create custom query using following filters query_vars, posts_join, posts_where, posts_groupby. here's the codex link to custom queries. Some examples are listed there for help.

share|improve this answer
add comment

You can do this with WP_Query using meta_query.

$my_query = new WP_Query( array(
    'meta_query' => array(
        array(
            'key' => 'date_field',
            'value => '03/11/2010',
            'compare' => '='
        )
    )
) );

By default it will search for published posts.

I would also suggest to store date in yyyy/mm/dd format. Sorting by this field will be much easier if you do this so.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.