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'm having a problem with an events system that I'm developing for a local roller derby leaugue. I have made a custom post type ('event') where a user can add a few different types of events (taxonomy: 'event-type', terms: 'bout', '*fresh_meat_day*' and '*public_event*'). A user can also enter a title, description, location and date for which I use a custom field (not the normal post date).

I display the events on a designated events page (events.php) and I want to show the first upcoming event for each category on my landing page. I have no problems with the events page. It shows a nice table displaying each upcoming event ordered by date (ASC). My problem is in the ordering of the events on the landing page.

I'm using this code to display the events on my landing page:

<?php 
$post_type = 'event';   
$tax = 'event-type';
$todaysDate = date('d.M.y');
$todaysString = strtotime($todaysDate);

$tax_terms = get_terms($tax, array('orderby' => 'id', 'order' => 'ASC'));
    if ($tax_terms) {
        foreach ($tax_terms as $tax_term) {

            $args = array(
                'post_type' => $post_type,
                "$tax" => $tax_term->slug,
                'post_status' => 'publish',
                'posts_per_page' => 1,
                'meta_key' => 'event_date',
                'meta_compare' => '=',
                'meta_value' => $todaysString,
                'orderby' => 'meta_value',
                'order' => 'ASC',
                'caller_get_posts' => 1
            ); // END $args

            $my_query = null;
            $my_query = new WP_Query($args);
            if ($my_query->have_posts()) { ;?>
                <ul>
                <?php while ($my_query->have_posts()) : $my_query->the_post();
                ?>
                    <li>
                        <div class="togglebox_140">
                            <img src="<?php bloginfo("template_directory"); ?>/images/events_freshmeat.png" />
                            <div class="togglebox_140_content">
                                <div class="togglebox_140_background">&nbsp;</div>
                                <div class="togglebox_text">
                                    <?php echo '<p class="togglebox_caption">Next '.$tax_term->name.'</p>';?>
                                    <p class="togglebox_text_type"><?php the_title(); ?></p>
                                </div>
                            </div> 
                        </div>
                    </li>
            <?php
                endwhile;
            } // END if have_posts loop?>
                </ul>
            <?php wp_reset_query();
        } // END foreach $tax_terms
    } // END if $tax_terms
?>

I get totally no output from this code except when I remove the line where it says

'meta_vale' => $todaysString;

If I remove that line, I get to see 3 upcoming events like it should and it shows the event name, but not in the right order (the event showing isn't necessarily the first upcoming event, but in stead the first event that I added).

I'm totally stuck on this one and I really need to get the events ordered in the right way. Any help would be greatly appreciated.

Cheers!

share|improve this question
3  
You're asking for "EVENTS = TODAY" and wondering why you get no output for upcoming events? –  kaiser May 18 '12 at 10:02
    
Wow, I can't believe I overlooked this... I changed 'meta_compare' from '=' to '>=' and the problem's solved... thanks for waking me up! ;) –  BraMKJ May 18 '12 at 12:33

1 Answer 1

As Kaiser has pointed out, the meta_value => $todaysString is querying for events where the value for key event_date is equal to todays string. I.e. events happening today.

Also, from the fact that you've used strtotime suggests you are sorting by timestamp - which you would like interpreted (and sorted as) a number rather than string. So rather than ordering by meta_vaue, you want to order by meta_value_num. Try:

        $args = array(
            'post_type' => $post_type,
            "$tax" => $tax_term->slug,
            'post_status' => 'publish',
            'posts_per_page' => 1,
            'meta_key' => 'event_date',
            'orderby' => 'meta_value_num',
            'order' => 'ASC',
        ); // END $args
share|improve this answer
    
Thanks a lot! Problem solved! :) –  BraMKJ May 18 '12 at 12:35

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.