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.

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:

<?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 \a\t 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' );
?>
share|improve this question
    
If you echo meta_value_num what does it say? Post date is the default order so the problem is most likely happening there. –  fdsa Jun 19 '12 at 22:29
add comment

1 Answer

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.

share|improve this answer
    
Doing this failed to return any posts at all. I've posted the contents of function.php. Would it be wiser to convert date input to UTC before input? If so, how would I be able to make that conversion within the context of the above code? –  user1456399 Jun 20 '12 at 0:58
    
@user1456399 you should convert your event_time values to mysql datetime format before saving it to the database -- as milo said, that's yyyy-mm-dd hh:mm:ss –  jessica Jun 20 '12 at 1:37
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.