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 need to change the posts in taxonomy.php page. I have a meta_key which i want to compare to meta value. But currently my code is not returning anything. I am sure i am missing something. Please let me know what i am doing wrong!

add_action('pre_get_posts', 'add_event_date_criteria');
function add_event_date_criteria(&$query)
{
    // We only want to filter on "public" pages
    // You can add other selections, like here:
    //  - Only on a term page for our custom taxonomy
    if (!is_admin() &&
        is_tax('event-tag') || is_tax('event-category')) {
        $query->set('meta_key', 'start_time');
        $query->set('meta_compare', '>=');
        $query->set('meta_value', time());
        $query->set('meta_key', 'start_time');
        $query->set('orderby', 'meta_value_num');
        $query->set('order', 'ASC');
    }

}
share|improve this question
add comment

1 Answer

up vote 6 down vote accepted

Try using meta_query parameter :

add_action('pre_get_posts', 'add_event_date_criteria');
function add_event_date_criteria(&$query)
{
    // We only want to filter on "public" pages
    // You can add other selections, like here:
    //  - Only on a term page for our custom taxonomy
    if (!is_admin() &&
        is_tax('event-tag') || is_tax('event-category')) {
        $time = time();
        $meta = array(
            array(
            'key' => 'start_time',
            'value' => $time,
            'compare' => '>='
            )
        );
        $query->set('meta_query',$meta );
        $query->set('meta_key', 'start_time');
        $query->set('orderby', 'meta_value_num');
        $query->set('order', 'ASC');
    }
}

also you need to make sure your meta field start_time is measured and saved in the number of seconds since the Unix Epoch like time() function.

share|improve this answer
    
yes.. the start_time is unix –  Sisir Apr 5 '11 at 21:36
    
OK, so does it Work for you? –  Bainternet Apr 5 '11 at 21:40
    
Sorry, it was very late last night.. I had to go to sleep; Your code works like a charm! Thanks! I have to check meta query in codex. Seems like it is new! –  Sisir Apr 6 '11 at 7:20
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.