EDIT: Reworded the question for more clarity
我正在尝试按自定义元框数据排序,我正在使用
strtotime
功能,但我有一些困难。
我的主要问题似乎是strtotime
作用如果我使用strtotime
在循环内部(见下文),我无法使用UNIX时间按日期排序。然而,如果我在保存时处理strotime(在functions.php中),那么如果用户编辑帖子或重新保存,我就会遇到一系列元框更新/不解析的问题。
有没有简单的解决方案?这是代码。。。我只想按自定义日期字段(而不是发布日期)升序排列这些帖子。
EDIT: Fixed the array by adding a meta-key - solution still pending
<!-- The args -->
<?php
$args = array(
"post_type" => "podcasts",
"meta-key" => "_date",
"orderby" => "meta_value",
"order" => "ASC"
);
?>
<!-- The Query -->
<?php $podcasts = new WP_Query( $args ); ?>
<!-- The Loop -->
<?php if ( $podcasts->have_posts() ) : while ( $podcasts->have_posts() ) : $podcasts->the_post(); ?>
<!-- Convert Date to Date Stamp -->
<?php $podcast_date = get_post_meta($post->ID, \'_date\', true);?>
<?php $fixed_date = strtotime($podcast_date); ?>
<!-- Content -->
EDIT: Added relevant meta-box code (below) from functions.php 注意——这段代码不包括strotime函数,因为我目前正在页面模板上使用它。我可以把它包括在这里,但我不知道该怎么做才能允许用户编辑帖子/重新保存。一旦strotime函数在保存时运行,它会不会将元框日期更改为1327248252之类的值?如果是这样的话,那么下次保存帖子时就不会进行解析,从而产生问题。
// Create podcast Meta boxes
function add_podcast_metaboxes() {
add_meta_box(\'podcast_info\', \'podcast Information\', \'podcast_info\', \'podcasts\', \'normal\', \'high\');
}
// podcast Meta Box
function podcast_info() {
global $post;
// Noncename needed to verify where the data originated
echo \'<input id="podcastmeta_noncename" name="podcastmeta_noncename" type="hidden" value="\' . wp_create_nonce( plugin_basename(__FILE__) ) . \'" />\';
// Get the data if its already been entered
$week = get_post_meta($post->ID, \'_week\', true);
$date = get_post_meta($post->ID, \'_date\', true);
$description = get_post_meta($post->ID, \'_description\', true);
// Echo out the field
echo \'<strong>Week</strong><br /><em>What week of the series is this podcast?</em>\';
echo \'<input class="widefat" name="_week" type="text" value="\' . $week . \'" />\';
echo \'<strong>Date</strong><br /><em>Enter the Date the podcast was recorded</em>\';
echo \'<input class="widefat" name="_date" type="text" value="\' . $date . \'" />\';
echo \'<strong>Description</strong><br /><em>Enter the text you would like to display as a description on the media archives list.</em>\';
echo \'<input class="widefat" name="_description" type="text" value="\' . $description . \'" />\';
}
// Save the podcast Meta box Data
function save_podcast_meta($post_id, $post) {
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST[\'podcastmeta_noncename\'], plugin_basename(__FILE__) )) {
return $post->ID;
}
// Is the user allowed to edit the post or page?
if ( !current_user_can( \'edit_post\', $post->ID ))
return $post->ID;
// OK, we\'re authenticated: we need to find and save the data
// We\'ll put it into an array to make it easier to loop though.
$podcast_meta[\'_week\'] = $_POST[\'_week\'];
$podcast_meta[\'_date\'] = $_POST[\'_date\'];
$podcast_meta[\'_thumbnail\'] = $_POST[\'_thumbnail\'];
$podcast_meta[\'_seriesimg\'] = $_POST[\'_seriesimg\'];
$podcast_meta[\'_description\'] = $_POST[\'_description\'];
// Add values of $podcast_meta as custom fields
foreach ($podcast_meta as $key => $value) { // Cycle through the $podcast_meta array!
if( $post->post_type == \'revision\' ) return; // Don\'t store custom data twice
$value = implode(\',\', (array)$value); // If $value is an array, make it a CSV (unlikely)
if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
update_post_meta($post->ID, $key, $value);
} else { // If the custom field doesn\'t have a value
add_post_meta($post->ID, $key, $value);
}
if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
}
}
add_action(\'save_post\', \'save_podcast_meta\', 1, 2); // save the custom fields
最合适的回答,由SO网友:Jared 整理而成
EDIT: 您的问题是:
保存元数据时,希望将日期另存为strtotime()
日期,但您希望它以旧格式显示日期Y-m-d
总体安排您需要做的是将其另存为strtotime()
然后,当在输入中显示回文本时,需要将其反转strtotime()
因此可以正确显示。您可以这样做:
在您的add_podcasts_metaboxes()
函数中,将获取日期的行更改为该行(如果有一个设置,则假定日期始终是UNIX时间戳):
$date = get_post_meta( $post->ID, \'_date\', true );
if( $date != \'\' )
$date = date( \'Y-m-d\', $date );
在中保存元数据时
save_podcast_meta()
, 你想怎么做就怎么做
strtotime()
:
$podcast_meta[\'_date\'] = strtotime( $_POST[\'_date\'] );
这会将日期转换为数据库中的UNIX时间戳,但
date()
函数,您可以将UNIX时间戳转换回您可以使用的日期。
您需要指定一个元键,然后指定orderbymeta_value
像这样:
<!-- The args -->
<?php
$args = array(
"post_type" => "podcasts",
"meta_key" => "some_meta_key", // Change to the meta key you want to sort by
"orderby" => "meta_value_num", // This stays as \'meta_value\' or \'meta_value_num\' (str sorting or numeric sorting)
"order" => "ASC"
);
?>
<!-- The Query -->
<?php $podcasts = new WP_Query( $args ); ?>
<!-- The Loop -->
<?php if ( $podcasts->have_posts() ) : while ( $podcasts->have_posts() ) : $podcasts->the_post(); ?>
<!-- Convert Date to Date Stamp -->
<?php $podcast_date = get_post_meta($post->ID, \'_date\', true);?>
<?php $fixed_date = strtotime($podcast_date); ?>
<!-- Content -->
参考请参见法典本页:
http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_ParametersEDIT: 我发现您刚才添加的元框代码有一个(可能)问题。但不是百分之百确定!
尝试替换:
echo \'<input id="podcastmeta_noncename" name="podcastmeta_noncename" type="hidden" value="\' . wp_create_nonce( plugin_basename(__FILE__) ) . \'" />\';
使用:
wp_nonce_field( plugin_basename( __FILE__ ), \'podcastmeta_noncename\' );
我不确定这是否会解决您的问题或产生很大的影响,但这就是我在元框中使用nonce的方式(尽管我相信还有另一种方法可以解决这个问题
Notice
如果你有
WP_DEBUG
设置为true)。