download2565 2016-02-27 19:09
浏览 148
已采纳

高级自定义字段Wordpress Repeater字段按日期排序

I am using advanced custom fields pro to store and display information on estate sale dates. These are held in a repeater field, each row in the field has a start time, end time and a date. I need to display the information ordered by the date in the repeater field row. Currently it is ordered by date modified. I have tried several solutions but none seem to work for what I need to do.

This is what I have currently:

<?php
$latestes = new WP_Query(
    array(
        'post_type' => 'estate-sales',
        'post_status' => 'publish',
        'posts_per_page' => 10,
        'orderby' => 'modified',
        'order' => 'DESC'
        )
    );

while ( $latestes->have_posts() ) : $latestes->the_post();
    echo '<li class="frontpage-esale"><a href="';
    the_permalink();
    echo '">';
    the_title();
    echo '&nbsp;';
    $rowses = get_field('estate_sale_dates');
    $first_row = $rowses[0];
    $first_row_date = $first_row['es-date'];
    $first_row_start = $first_row['es-start-time'];
    $first_row_end = $first_row['es-end-time'];
    echo '&nbsp;-&nbsp;';
    if ($first_row_date) {
        echo date('F j, Y',strtotime($first_row_date));
    }
    else {
    echo 'Sale Dates TBA';
    }
endwhile;
wp_reset_query();
?>

The most common answer when searching for a solution is to do something like this:

$latestes = new WP_Query(
    array(
        'post_type' => 'estate-sales',
        'post_status' => 'publish',
        'posts_per_page' => 10,
        'orderby' => 'modified',
        'order' => 'DESC'
        )
    );

while ( $latestes->have_posts() ) : $latestes->the_post();
$repeater = get_field('estate_sale_dates');
    foreach( $repeater as $key => $row )
    {
        $column_id[ $key ] = $row['es-date'];
    }
    array_multisort( $column_id, SORT_ASC, $repeater );
    foreach( $repeater as $row ) {
    echo '<li class="frontpage-esale"><a href="';
    the_permalink();
    echo '">';
    the_title();
    echo '&nbsp;';
    $rowses = get_field('estate_sale_dates');
    $first_row = $rowses[0];
    $first_row_date = $first_row['es-date'];
    $first_row_start = $first_row['es-start-time'];
    $first_row_end = $first_row['es-end-time'];
    echo '&nbsp;-&nbsp;';
    if ($first_row_date) {
        echo date('F j, Y',strtotime($first_row_date));
    }
    else {
    echo 'Sale Dates TBA';
    }
    }
endwhile;
wp_reset_query();
?>

However, when I try that, it lists each sale 3 times, does not order by the date at all, and if no date is entered (which needs to just default to Sale Dates TBA) it throws an error.

Any help greatly appreciated.

  • 写回答

2条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥20 b站私信完整导出的方法
      • ¥15 边界图中的x、y轴解释
      • ¥30 如何用matlab求解复杂方程
      • ¥15 VUE TS中怎么调用第三方js
      • ¥15 Activiti如何将值传入textValue
      • ¥20 在linux里安装lummps时 在make mpi里找不到rpc
      • ¥25 python先到先得调度和轮询调度
      • ¥15 SDK Manager闪退
      • ¥15 intel i219v mac地址修改问题
      • ¥15 leetcode上面的题,求解