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