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条回答 默认 最新

  • duanjiati1755 2016-02-27 20:46
    关注

    Note, that your integration isn't complete, signaled by having two get_field('estate_sale_dates');, and the name $first_row being out of place when iterating all of them. Without the sort, you're basically doing

      $repeater = get_field( 'estate_sale_dates' );
      ...
      foreach ( $repeater => $row_that_isnt_used )
      {
         $rowses = get_field( 'estate_sale_dates' );
         $first_row = $rowses[0]; 
      }
    

    I think something like this is what you're after:

    <?php
    while ( $latestes->have_posts() ) {
                    $latestes->the_post();
        echo '<li class="frontpage-esale"><a href="';
        the_permalink();
        echo '">';
        the_title();
        echo '</a> ';
    
        if ( ! count( $sale_dates = get_field('estate_sale_dates') ) )
            echo '<span>Sale Dates TBA</span>';
        else
        {
            // $order = array_column( $sale_dates, 'es-date' ): (php 5.5+)
            foreach( $sale_dates as $ignore => $row )
                $order[] = $row['es-date'];
    
            array_multisort( $order, SORT_ASC, $sale_dates );
    
            echo "<ul>";
            foreach( $sale_dates as $row ) {
                $date  = $row['es-date'];
                $start = $row['es-start-time'];
                $end   = $row['es-end-time'];
                echo "<li>" . date('F j, Y',strtotime($row_date)) . " $start-$end</li>";
            }
            echo "</ul>";
        }
    
        echo "</li>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?