我有一个带有自定义元数据库的自定义帖子类型(as seen on wptheming.com) 并且希望进行查询以显示即将发生的事件,例如在侧边栏中,过去的日期被忽略。
但我需要一个当前时间的值来和事件开始时间进行比较。
现在我从current_time(\'mysql\')
看起来是这样的:
2012-02-16 13:15:31
元的开始时间如下所示:
201108121100
如何使这两个日期具有可比性?我可以在查询中执行吗?或者是否有其他解决方案?
以下是我到目前为止的查询(值留空,时间戳回显):
<?php $args = array(
\'post_type\' => \'event\',
\'meta_key\' => \'_start_eventtimestamp\',
\'orderby\' => \'meta_value_num\',
\'meta_query\' => array(
array(
\'key\' => \'_start_eventtimestamp\',
\'value\' => \'\',
\'compare\' => \'!=\',
\'type\' => \'NUMERIC\'
)
),
\'order\' => \'ASC\',
\'posts_per_page\' => 10,
);
$events = new WP_Query( $args );
if ( $events->have_posts() ) :
echo \'<ul>\';
while ( $events->have_posts() ) : $events->the_post();
echo \'<li><a href="\' . get_permalink() . \'">\' . current_time(\'mysql\') . \'</a></li>\';
echo \'<li><a href="\' . get_permalink() . \'">\' . get_post_meta( $post->ID, \'_start_eventtimestamp\', true ) . \'</a></li>\';
endwhile;
echo \'</ul>\';
endif;
?>
下面是我函数中的代码。php。(它可以在archive-event.php中正常工作,但在其他任何地方都不行):
function ep_event_query( $query ) {
// http://codex.wordpress.org/Function_Reference/current_time
$current_time = current_time(\'mysql\');
list( $today_year, $today_month, $today_day, $hour, $minute, $second ) = split( \'([^0-9])\', $current_time );
$current_timestamp = $today_year . $today_month . $today_day . $hour . $minute;
global $wp_the_query;
if ( $wp_the_query === $query && !is_admin() && is_post_type_archive( \'event\' ) ) {
$meta_query = array(
array(
\'key\' => \'_start_eventtimestamp\',
\'value\' => $current_timestamp,
\'compare\' => \'>\'
)
);
$query->set( \'meta_query\', $meta_query );
$query->set( \'orderby\', \'meta_value_num\' );
$query->set( \'meta_key\', \'_start_eventtimestamp\' );
$query->set( \'order\', \'ASC\' );
}
}
add\\u操作(“pre\\u get\\u posts”、“ep\\u event\\u query”);
我该怎么做?