douguo6472 2014-04-07 18:47
浏览 49
已采纳

排序Wordpress帖子时间戳

I have a custom post type of events. I want to display the event only if its today or upcoming.

The problem i'm having is if I have multiple events, the order isnt going according to the date.

I did try echoing the timestamp and it was off - it needs to be converted to php timestamp - is there a way to divide the timestamp by 1000 in jquery before it saves it? or am I totally off here

any help would be greatly appreciated!

Custom field

<input type="text" class="datepicker" size="30" />
<input type="hidden" id="event_date" size="30" />

datepicker js

jQuery(document).ready(function(){
        jQuery('.datepicker').datepicker({
            dateFormat : 'DD, MM dd',
            altField: "#event_date",
        altFormat: '@'
        });
});

Callback php

<?php $classarg = array('post_type' => 'class', 'meta_query' => array(array('key' => 'event_date', 'value' => time(), 'compare' => '>=',),),'orderby'=> 'key', 'posts_per_page'=> '-1', 'order'=>'ASC');
$classes = new WP_Query($classarg ); if ( $classes->have_posts() ) {while ( $classes->have_posts() ) { $classes->the_post();?>

<h2><?php the_title();?></h2>

<?php the_content();?>

<?php }} wp_reset_postdata();?>
  • 写回答

1条回答 默认 最新

  • dongzhi6905 2014-04-08 17:53
    关注

    I realized I was missing meta_key parameter and orderby has to be meta_value_num

    So this works now

     <?php
        $classarg= array(
            'post_type' => 'class',
            'posts_per_page' => -1,
            'nopaging' => true,
            'post_status' => 'publish',
            'meta_key' => 'event_date',
            'meta_query' => array(array('key' => 'event_date', 'value' => time(), 'compare' => '>=',),),
            'orderby'=> 'meta_value_num',
            'order' => 'ASC'
        );
        $classes = new WP_Query($classarg  ); 
        if ( $classes->have_posts() ): while ( $classes->have_posts() ) : $classes->the_post();?>
    
        <h2><?php the_title();?></h2>
    
        <?php the_content();?>
    
        <?php endwhile; endif; wp_reset_postdata();?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?