按元字段日期排序自定义帖子

时间:2014-04-25 作者:user50806

我在黑板上尝试了各种例子的组合,但似乎没有什么能满足我的需要。我有一个事件发布类型,在自定义元框中有多个字段,其中一个是以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 );

1 个回复
SO网友:cybmeta

您没有查询/保存正确的meta_key. 例如,查看您的代码,您通过$typename 变量至update_post_meta()meta_key$typename = \'events\'. 但稍后在您要查询的查询中meta_key = date. 还有,你通过了$metameta_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 );

结束

相关推荐

使用自定义字段时复制_DATE函数

我过去常常使用发布日期对页面上的帖子进行排序。现在,我正在使用自定义日期/时间字段对帖子进行排序。在使用发布日期时,我使用了\\u date函数,如果两篇帖子在同一天,该函数只显示一次日期。例如:March 14 Headline for Post 1 Headline for Post 2 (second post on the same day) March 13 Headline for post 3 (only one post on this day