[英]WordPress loop sort by custom date
我有一個事件自定義帖子類型,但是想按日期對帖子進行排序,這是一個自定義字段。 我已經嘗試了一些在$ args中使用meta的方法,但無法正確完成。 有人有什么建議嗎?
<?php
$args = array(
'post_type' => 'fw-event',
'posts_per_page' => -1
);
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) :
$the_query->the_post();
global $post;
$options = fw_get_db_post_option($post->ID, fw()->extensions->get( 'events' )->get_event_option_id());
$eventbrite = fw_get_db_post_option($post->ID, 'eventbrite');
$eventlogo = fw_get_db_post_option($post->ID, 'eventlogo');
$test = fw_get_db_post_option($post->ID, 'event_date_range');
// Set variables for date
if ($options['event_children']) {
foreach($options['event_children'] as $key => $row) :
$originalDate = $row['event_date_range']['from'];
$newDate = date("d M, Y", strtotime($originalDate));
$Day = date("d", strtotime($originalDate));
$Month = date("M", strtotime($originalDate));
endforeach;
}
?>
<?php if (strtotime($newDate) > time() && $options['event_children']) { ?>
<div class="event-listing">
<div class="date matchHeightRow">
<?php if ($options['event_children']) { ?>
<?php foreach($options['event_children'] as $key => $row) : ?>
<span><?php echo esc_attr__($Day); ?></span>
<?php echo esc_attr__($Month); ?>
<?php endforeach; ?>
<?php } ?>
</div>
<div class="content matchHeightRow">
<aside>
<h5>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php the_title(); ?>
</a>
</h5>
<?php echo limit_words(get_the_excerpt(), '15'); ?>…
<p>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php echo esc_attr__( 'Find out more →' , 'wmmc' ); ?>
</a>
</p>
</aside>
</div>
</div>
<?php } ?>
<?php endwhile; ?>
如您所見,我正在使用變量拆分日期,謝謝!
使用strtotime()
將日期保存在數據庫中,您將存儲日期的int值。 這應該工作
$args = array(
'post_type' => 'fw-event',
'posts_per_page' => - 1,
'meta_key' => 'my_date',
'order' => 'DESC',
'orderby' => 'meta_value_num',
);
這種方法對我來說很好。
如果您有日期“從”和日期“至”-將其保存到其他字段。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.