Ordering posts by date custom field

Trying to order posts by custom field. Dates are entered into the custom field “event_time” as in this format: “June 6 2012 6:00 PM”. Trying to order them by that date. All of the code below functions properly, but the posts are in order of post date, not “event_time”. Where is my issue?

Contents of archive-event.php:

Read More
<?php get_header(); ?>

<?php
$args = array(
    "post_type" => "event",
    "meta_key" => strtotime('event_time'),
    "orderby" => "meta_value_num",
    "order" => "ASC"
    );
$event = new WP_Query( $args );
?>

<div id="wrapper">

<div id="content">

    <?php if ($event->have_posts()): ?>

    <div id="posts">

        <?php while ($event->have_posts()) : $event->the_post(); ?>

            <?php $get_event = get_post_meta( $post->ID, 'event', true ); ?>

            <a href="<?php echo $get_event[ 'event_url' ]; ?>" rel="bookmark">

                <?php the_title(); ?><br />
                <?php echo $get_event[ 'event_venue' ]; ?><br />
                <?php echo $get_event[ 'event_address' ]; ?><br />
                <?php echo date( 'l, F j, Y at g:i A', strtotime($get_event[ 'event_time' ])); ?><br />

            </a>

        <?php endwhile; ?>

    </div>

<?php else: ?>

    <p>Sorry, no posts matched your criterias.</p>

<?php endif; ?>

<?php if(function_exists('wp_paginate')) { wp_paginate(); } ?>

    </div><!-- @end content -->

    <div id="aside">
    <?php get_sidebar(); ?>
    </div><!-- @end aside -->

</div><!-- @end wrapper -->

<?php get_footer(); ?>

Relevant Contents of Functions.php

$key = "event";
$meta_boxes = array(
    "event_url" => array(
        "name" => "event_url",
        "title" => "Meetup Event Url",
        "description" => "Enter the URL of the Meetup event, e.g. http://www.meetup.com/meetup-group/events/58276792/"),
    "event_venue" => array(
        "name" => "event_venue",
        "title" => "Event Venue",
        "description" => "Enter the event venue of the meetup, e.g. Granite City Brewery"),
    "event_address" => array(
        "name" => "event_address",
        "title" => "Event Address",
        "description" => "Enter the address of the venue, e.g. 25 Clair St., Denver , CO"),
    "event_time" => array(
        "name" => "event_time",
        "title" => "Event Date & Time",
        "description" => "Enter the date and time of the event in the following format, e.g. June 6 2012 6:00 PM")
);

function create_meta_box() {
    global $key;

    if( function_exists( 'add_meta_box' ) ) {
        add_meta_box( 'new-meta-boxes', ucfirst( $key ) . ' Details', 'display_meta_box', 'event', 'normal', 'high' );
        }
    }

function display_meta_box() {
    global $post, $meta_boxes, $key;
    ?>

    <div class="form-wrap">

    <?php

    wp_nonce_field( plugin_basename( __FILE__ ), $key . '_wpnonce', false, true );

    foreach($meta_boxes as $meta_box) {
        $data = get_post_meta($post->ID, $key, true);
        ?>

        <div class="form-field form-required">

            <label for="<?php echo $meta_box[ 'name' ]; ?>"><?php echo $meta_box[ 'title' ]; ?></label>

                <input type="text" name="<?php echo $meta_box[ 'name' ]; ?>" value="<?php echo htmlspecialchars( $data[ $meta_box[ 'name' ] ] ); ?>" />

            <p><?php echo $meta_box[ 'description' ]; ?></p>

        </div>

    <?php } ?>

    </div>

<?php
}

function save_meta_box( $post_id ) {
    global $post, $meta_boxes, $key;

    foreach( $meta_boxes as $meta_box ) {
        $data[ $meta_box[ 'name' ] ] = $_POST[ $meta_box[ 'name' ] ];
    }

    if ( !wp_verify_nonce( $_POST[ $key . '_wpnonce' ], plugin_basename(__FILE__) ) )
        return $post_id;

    if ( !current_user_can( 'edit_post', $post_id ))
        return $post_id;

    update_post_meta( $post_id, $key, $data );
}

add_action( 'admin_menu', 'create_meta_box' );
add_action( 'save_post', 'save_meta_box' );
?>

Related posts

Leave a Reply

1 comment

  1. In your $args array, "meta_key" should be "event_time". If you want to get events after a certain date you also have to set meta_value to the desired date and meta_compare to >, <, etc..

    Note however that you’re not going to be able to order the posts with the format "June 6 2012 6:00 PM", this isn’t a format that MySQL understands. You should be using the native datetime format yyyy-mm-dd hh:mm:ss.