按日期对帖子排序自定义域

时间:2012-06-19 作者:danpaquette

正在尝试按自定义字段排序帖子。在自定义字段“event\\u time”中输入日期,格式如下:“2012年6月6日下午6:00”。试图在那个日期前订购。下面的所有代码都正常工作,但帖子是按发布日期排序的,而不是“event\\u time”。我的问题在哪里?

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\' );
?>

1 个回复
SO网友:Milo

在您的$args 大堆"meta_key" 应该是"event_time". 如果要在特定日期之后获取事件,还必须设置meta_value 至所需日期,以及meta_compare 到>,<;,等

但是请注意,您将无法使用该格式订购帖子"June 6 2012 6:00 PM", MySQL不理解这种格式。您应该使用本机datetime格式yyyy-mm-dd hh:mm:ss.

结束

相关推荐