我在黑板上尝试了各种例子的组合,但似乎没有什么能满足我的需要。我有一个事件发布类型,在自定义元框中有多个字段,其中一个是以yyyymmdd格式保存的日期字段。我只需要查询今天和将来发生的事件,并在ASC order by date字段中排序。这将在自定义帖子类型存档页面中使用。
Metabox功能:
function events_metabox() {
global $post;
$typename = \'events\';
$meta = get_post_meta($post->ID, $typename, true);
$date = ( !empty( $meta[\'date\'] ) ) ? $meta[\'date\'] : \'\';
$start = ( !empty( $meta[\'start\'] ) ) ? $meta[\'start\'] : \'\';
$end = ( !empty( $meta[\'end\'] ) ) ? $meta[\'end\'] : \'\';
$location = ( !empty( $meta[\'location\'] ) ) ? $meta[\'location\'] : \'\';
$form = ( !empty( $meta[\'form\'] ) ) ? $meta[\'form\'] : \'\';
wp_nonce_field( $typename, $typename . "-nonce" );
?>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery(\'#events_date\').datepicker({
dateFormat : \'yymmdd\'
});
});
</script>
<div class="field">
<label for="<?php echo $typename; ?>_address">Location:</label>
<textarea id="<?php echo $typename; ?>_address" name="<?php echo $typename; ?>[location]" class="widefat"><?php echo $location; ?></textarea>
<em class="field-summary summary">Event location.</em>
</div>
<div class="field">
<label for="<?php echo $typename; ?>_phone">Event Date:</label>
<input type="text" id="<?php echo $typename; ?>_date" name="<?php echo $typename; ?>[date]" value="<?php echo $date; ?>" />
<em class="field-summary summary">Event date.</em>
</div>
<div class="field">
<label for="<?php echo $typename; ?>_email">Start Time:</label>
<input type="text" id="<?php echo $typename; ?>_start" name="<?php echo $typename; ?>[start]" value="<?php echo $start; ?>" />
<em class="field-summary summary">Start time of event.</em>
</div>
<div class="field">
<label for="<?php echo $typename; ?>_email">End Time:</label>
<textarea id="<?php echo $typename; ?>_end" name="<?php echo $typename; ?>[end]"><?php echo $end; ?></textarea>
<em class="field-summary summary">End time of event.</em>
</div>
<div class="field">
<label for="<?php echo $typename; ?>_form">Web Form:</label>
<textarea id="<?php echo $typename; ?>_form" name="<?php echo $typename; ?>[form]" class="widefat"><?php echo $form; ?></textarea>
<em class="field-summary summary">Web Form.</em>
</div>
<?php } ?>
保存自定义元字段的函数:
function events_save_meta($post_id, $post) {
$typename = \'events\';
$id = $post->ID;
if(!isset($_POST[$typename])){
return $id;
}
$meta = $_POST[$typename];
if (!wp_verify_nonce($_POST[$typename . "-nonce"], $typename)) return $id;
if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE )
return $id;
if( $typename != $_POST[\'post_type\'] )
return $id;
update_post_meta($id, $typename, $meta);
}
后期查询:
$args = array(
\'post_type\' => \'events\',
\'meta_key\' => \'date\',
\'orderby\' => \'meta_value_num\',
\'order\' => \'ASC\',
\'meta_query\' => array(
array(
\'key\' => \'date\',
\'value\' => date(\'Ymd\'),
\'compare\' => \'>=\',
\'type\' => \'NUMERIC\'
)
)
);
$events_query = new WP_Query( $args );
SO网友:cybmeta
您没有查询/保存正确的meta_key
. 例如,查看您的代码,您通过$typename
变量至update_post_meta()
像meta_key
和$typename = \'events\'
. 但稍后在您要查询的查询中meta_key = date
. 还有,你通过了$meta
像meta_value
在你的实际代码中$meta
是一个包含多个值(位置、日期、开始等)的数组。
$typename = \'events\';
//$_POST[$typename] is actually an array
$meta = $_POST[$typename];
......
.....
update_post_meta($id, $typename, $meta);
如果您不接触元框的HTML,则必须更改保存代码和查询。例如:
add_action(\'save_post\', \'events_save_meta\');
function events_save_meta($post_id) {
$typename = \'events\';
if ( isset($_POST[\'post_type\']) && \'events\' == $_POST[\'post_type\'] ) {
//You may need to change \'edit_post\' for the correct capability set when register \'events\' post type
if ( !current_user_can( \'edit_post\', $post_id ) ) {
return;
}
}
if( !isset( $_POST[$typename . "-nonce"] ) || !wp_verify_nonce($_POST[$typename . "-nonce"], $typename) ) {
return;
}
if( isset($_POST[$typename][\'date\']) ) {
update_post_meta($post_id, \'date\', $_POST[$typename][\'date\']);
} else {
delete_post_meta($post_id, \'date\');
}
}
现在您有了一个元字段
meta_key = \'date\'
你可以查询这个
meta_key
:
$args = array(
\'post_type\' => \'events\',
\'meta_key\' => \'date\',
\'meta_value\' => current_time(\'Ymd\'),
\'meta_compare\' => \'>=\',
\'orderby\' => \'meta_value_num\',
\'order\' => \'ASC\'
);
$events_query = new WP_Query( $args );