按ACF Datepicker排序WP_Query

时间:2015-03-09 20:43:29

标签: php wordpress datepicker unix-timestamp advanced-custom-fields

我有一个“即将发生的事件”页面和一个“过去的事件”页面。每个事件都有一个名为“event_date”的自定义字段。

我想创建一个显示比今天更大的所有事件的循环。我已经看了这些文章,但无法让它工作: http://support.advancedcustomfields.com/forums/topic/how-do-i-filter-and-sort-event-posts-with-start-and-end-date/

https://wordpress.org/support/topic/plugin-advanced-custom-fields-sorting-by-date-picker

wordpress advanced custom fields order posts by date-picker

从我在上面三个链接中收集的内容中,我将把它放在我的functions.php文件中:

    // CREATE UNIX TIME STAMP FROM DATE PICKER
function custom_unixtimesamp ( $post_id ) {
    if ( get_post_type( $post_id ) == 'event_type' ) {
    $event_date = get_post_meta($post_id, 'event_date', true);

        if($event_date) {
            $dateparts = explode('/', $event_date);
            $newdate1 = strtotime(date('d.m.Y H:i:s', strtotime($dateparts[1].'/'.$dateparts[0].'/'.$dateparts[2])));
            update_post_meta($post_id, 'unixstartdate', $newdate1  );
        }
    }
}
add_action( 'save_post', 'custom_unixtimesamp', 100, 2);

然后我会在页面模板中添加这样的内容:

<?php 
$today = time();
$args = array(
        'post_type' => 'event_type',
        'posts_per_page' => 5,
        'meta_query' => array(
            array(
            'key' => 'unixstartdate',
            'compare' => '>=',
            'value' => $today,
            )
        ),
        'meta_key' => 'event_date',
        'orderby' => 'meta_value',
        'order' => 'ASC',
    );

$query = new WP_Query( $args );
$event_type = $query->posts;
?>

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

现在没有出现任何结果。我的post-type称为“event_type”,键是“event_date”。

关于我哪里出错的任何想法?

3 个答案:

答案 0 :(得分:4)

由于 svsdnb。

,我在这里找到了解决方案

https://wordpress.org/support/topic/query-date-array-to-display-future-events-only

除了必须转换functions.php中的时间戳之外,还有一种方法可以使用ACF专门执行此操作

current_time('Ymd')

而不是

 $today = date ('Ymd')

这是我最终得到的结果(它似乎正在发挥作用,包括今天发生的事件):

<?php 
$today = current_time('Ymd');
$args = array(
    'post_type' => 'event_type',
    'post_status' => 'publish',
    'posts_per_page' => '0',
    'meta_query' => array(
        array(
            'key' => 'event_date',
            'compare' => '>=', // Upcoming Events - Greater than or equal to today
            'value' => $today,
        )
    ),
    'meta_key' => 'event_date',
    'orderby' => 'meta_value',
    'order' => 'ASC',
    );

$query = new WP_Query( $args );
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
?>

答案 1 :(得分:2)

您的代码看起来有些问题。您的日期解析似乎也不正确,但如果没有更多信息,很难说。在设置strtotime的函数和查询代码中都使用unixstarttime会很好。您的代码使用time(),其中包括&#34;今天&#34;的秒数和遗漏事件。你没有时区,所以一切都将被视为GMT,只要你记住这一点就没问题。

不是很重要,但是你在add_action中为你的回调指定了两个参数,但该函数只接受一个参数。

下一个问题是have_posts()循环 - 您需要指定自定义查询$query来循环它。

<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>

我还使用updated_{type}_meta操作以稍微不同的方式实现它,只触发正确的元键,然后检查帖子类型。它应该仅在更新元值时运行,而不是每次保存帖子时运行。我还建议按unixstarttime元值进行排序,因为它是数字。

<强>的functions.php

// create/update unixstartdate based on event_type.event_date update
add_action( 'updated_post_meta', 'my_updated_post_meta', 20, 4 );
function my_updated_post_meta( $meta_id, $object_id, $meta_key, $_meta_value ){
    if ( $meta_key == 'event_date' && 'event_type' == get_post_type( $object_id ) ){
        $unixstartdate = strtotime( $_meta_value );
        update_post_meta( $object_id, 'unixstartdate', $unixstartdate );
    }
}

模板页面

$args = array(
    'post_type' => 'event_type',
    'posts_per_page' => 5,
    'meta_query' => array(
        array(
            'key' => 'unixstartdate',
            'compare' => '>=',
            'value' => strtotime('m/d/Y', time()),
        )
    ),
    'meta_key' => 'unixstartdate',
    'orderby' => 'meta_value',
    'order' => 'ASC',
);

$query = new WP_Query( $args );
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
    the_title(); // whatever you want to use from $post
endwhile; endif;

答案 2 :(得分:-1)

<div class="row">

<?php 
$today = current_time('Ymd');
$args = array(
    'post_type' => 'your-custom-post-type',
    'post_status' => 'publish',
    'posts_per_page' => '-1',
    'meta_query' => array(
        array(
            'key' => 'event_date',//your date picker field name
            'compare' => '>=', // Upcoming Events - Greater than or equal to today
            'value' => $today,
        )
    ),
    'meta_key' => 'event_date',
    'orderby' => 'meta_value',
    'order' => 'Desc',
    );

$query = new WP_Query( $args );
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
?>
<?php  $dateformatstring = "F d, Y";

$unixtimestamp = strtotime(get_field('event_date')); ?>

<div class="post">

    <a href="<?php the_permalink; ?>"><?php the_title(); ?></a>
 <?php echo date_i18n($dateformatstring, $unixtimestamp); ?>

</div>

 <?php
  endwhile;
endif;
wp_reset_query();  // Restore global post data stomped by the_post().
?>

</div>
相关问题