按事件日期排序事件,而不是发布日期

时间:2016-10-13 17:14:54

标签: wordpress events

Here's the page I'm working on.

这就是我的尝试,但它不会验证。

$today = date('Ymd');
$args = array(
'post_type'         => 'events',
'posts_per_page'    => 10,
'meta_key'      => 'calendar_date', // name of custom field
'orderby'       => 'meta_value_num',
'order'             => 'ASC',
'meta_query' => array(
array(
'key'       => 'calendar_date',
'compare'   => '<=',
'value'     => $today,
)
),
);

$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post(); 

1 个答案:

答案 0 :(得分:0)

您是否在事件页面中使用此代码?

<?php
$today = date('m/d/Y');
$args = array(
    'post_type'         => 'events',
    'posts_per_page'    => 10,
    'orderby'       => 'meta_value_num',
    'order'             => 'ASC',
    'meta_key'      => 'calendar_date', // name of custom field
    'meta_compare'   => '<=',
    'meta_value'     => $today
);

$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        $convert = strtotime( get_field('calendar_date') );
        $calendarDate = date('M d', $convert);
        ?>
        <div class="news-date">
            <p><?php echo $calendarDate; ?></p>
            <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
            <i class="fa fa-2x fa-angle-right"></i>
        </div>
    <?php } ?>
<?php } ?>