• Ok, so I’m using custom fields to add “event dates” for upcoming events on a blog. First let me say I’m not looking for plugin suggestions, they’re all bloated and complex.

    I have a sidebar with a custom loop that runs like this

    <?php
                $recentPosts = new WP_Query();
                $recentPosts->query('showposts=5meta_key=event_date&orderby=meta_value&category_name=events&order=ASC');
    		if ($recentPosts->have_posts()) : while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
    
                <li><?php  if((get_post_meta($post->ID, "event_date", true))) { ?><span class="event_date">
                            <?php echo get_post_meta($post->ID, "event_date", true); ?>		
    
                    <?php } ?> <!-- END event_date --></span>
    
                    <a class="event_title" href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
    
        	<?php endwhile; else: ?>
    			 <li><?php _e('No upcoming Events'); ?></li>
    		<?php endif; ?>

    The trouble that I have is that when I sort by the custom field “event_date” naturally it sorts my ‘Feb 08’, ‘Mar 28’, etc. dates alphabetically. Is there a way to somehow embed date information in these custom fields so that I can have real dates and use PHP the_date to call them as ‘M d’ in one place and ‘m D, Y’ in another?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter brianfeister

    (@brianfeister)

    For people with my problem – I’m not a PHP wizard so there may be a way to do this, but what has worked better for me than all the plugins is to have 3 custom fields for each event. One is event_date which is a raw numeric ‘mmddyy’. I use this to set ‘orderby=meta_key’ and order the posts without displaying the ugly date. Then I have ‘short_date’ and ‘long_date’. Short date shows ‘Mar 19’ in the sidebar and Long date shows ‘March 19, 2010 5:00-7:00pm’ in single.php. Here is my code, hope it helps someone:

    <?php
                $recentPosts = new WP_Query();
                $recentPosts->query('showposts=5&meta_key=event_date&orderby=meta_value&category_name=events&order=ASC');
    		if ($recentPosts->have_posts()) : while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
    
                <li><?php  if((get_post_meta($post->ID, "short_date", true))) { ?><span class="event_date">
                            <?php echo get_post_meta($post->ID, "short_date", true); ?>		
    
                    <?php } ?> <!-- END event_date --></span>
    
                    <a class="event_title" href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
    
        	<?php endwhile; else: ?>
    			 <li><?php _e('No upcoming Events'); ?></li>
    		<?php endif; ?>

    The trouble that I have is that when I sort by the custom field “event_date” naturally it sorts my ‘Feb 08’, ‘Mar 28’, etc. dates alphabetically.

    One is event_date which is a raw numeric ‘mmddyy’

    Even in that format, ‘mmddyy’, you aren’t aren’t going to get an accurate sort. You’d need something like ‘yyyymmdd’ instead.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom Fields and PHP the_date’ is closed to new replies.