2016-02-27 66 views
1

我使用高级自定义字段亲来存储和显示有关房地产销售日期的信息。这些保存在中继器字段中,字段中的每一行都有一个开始时间,结束时间和日期。我需要在中继器字段行中显示按日期排序的信息。目前它是按修改日期排序的。我尝试了几种解决方案,但似乎没有任何工作需要我做。高级自定义字段WordPress的中继器字段排序依据日期

这是我目前:

<?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(); 
?> 

一个解决方案搜索时最常见的回答是做这样的事情:

$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(); 
?> 

然而,当我尝试,它列出每次销售3次,根本不按日期排序,如果没有输入日期(只需要默认销售日期待定),则会引发错误。

任何帮助非常感谢。

+0

为什么你要传递''orderby'=>'modified',order =>'DESC''? – Kenney

+0

只是一般格式来启动WP_Query并拉出所有帖子。 – Xype

+0

由于它是中继器字段中的子字段,因此无法按WP_Query数组内的$ key和元数字顺序进行排序。 – Xype

回答

1

请注意,您的集成不完整,通过两个get_field('estate_sale_dates');$first_row迭代所有这些名称时出现错误信号。如果没有排序,你基本上做

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

我觉得这样的事情是你追求的:

<?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>"; 
} 
+0

尝试这个我只是得到吨错误,不支持的运营商等。 – Xype

+0

修正了错别字。 – Kenney

+0

错别字不是问题,我已经修复了这些问题,在拼写错误上没什么大不了的。但是我得到这个致命错误:[]运算符不支持字符串。与$ order [] = $ row ['es-date']相关联 – Xype

-1

我有一个日历同样的问题。有没有解决方案,以正确的方式与repater字段做到这一点。做到这一点的最佳方式是在隐藏帖子类型和关系字段中使用自定义字段。

因为任何PHP解决方案都不能执行。

在自定义字段“日期”和第二个“发布关系”中创建自定义帖子类型“销售日期”。 然后从帖子类型“销售日期”中选择所有帖子通过元关键字“日期”与关系字段您从非隐藏帖子类型中获取数据。

+0

具有日期的ACF字段的自定义帖子类型将工作。我让它在客户端上运行。 – Aibrean

+0

但ACF Repeater Fied中的ACF字段不适用于排序。在最好的情况下,它不起作用。之间有差别。 – Skrabbel

+0

你所做的只是按元键排序。这并不难。例如:https://www.advancedcustomfields.com/resources/orde-posts-by-custom-fields/。作为自定义帖子类型 - 没有问题。作为中继器 - 不起作用。 – Aibrean

相关问题