高级自定义字段WordPress的中继器字段排序依据日期
问题描述:
我使用高级自定义字段亲来存储和显示有关房地产销售日期的信息。这些保存在中继器字段中,字段中的每一行都有一个开始时间,结束时间和日期。我需要在中继器字段行中显示按日期排序的信息。目前它是按修改日期排序的。我尝试了几种解决方案,但似乎没有任何工作需要我做。高级自定义字段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 ' ';
$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 ' - ';
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 ' ';
$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 ' - ';
if ($first_row_date) {
echo date('F j, Y',strtotime($first_row_date));
}
else {
echo 'Sale Dates TBA';
}
}
endwhile;
wp_reset_query();
?>
然而,当我尝试,它列出每次销售3次,根本不按日期排序,如果没有输入日期(只需要默认销售日期待定),则会引发错误。
任何帮助非常感谢。
答
请注意,您的集成不完整,通过两个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>";
}
答
我有一个日历同样的问题。有没有解决方案,以正确的方式与repater字段做到这一点。做到这一点的最佳方式是在隐藏帖子类型和关系字段中使用自定义字段。
因为任何PHP解决方案都不能执行。
在自定义字段“日期”和第二个“发布关系”中创建自定义帖子类型“销售日期”。 然后从帖子类型“销售日期”中选择所有帖子通过元关键字“日期”与关系字段您从非隐藏帖子类型中获取数据。
为什么你要传递''orderby'=>'modified',order =>'DESC''? – Kenney
只是一般格式来启动WP_Query并拉出所有帖子。 – Xype
由于它是中继器字段中的子字段,因此无法按WP_Query数组内的$ key和元数字顺序进行排序。 – Xype