John Davidson

php - WordPress ACF - Find next date in repeater that is not past

0 comments
Message:


I need some assistance on how to query posts using an ACF repeater that contains 'DateTime' fields and return all the posts that haven't already passed. Once I get that query, then display ONLY the DateTime for the next upcoming DateTime.


I have this created for a simple non-repeating TimeDate field query that will grab all the posts that have a start_date after today, but I am not sure how to convert it into filtering using the repeater instead:


$date_now = date('Y-m-d H:i:s');
$time_now = strtotime($date_now);

$args = array(
'post_type' => array('events-schedule'),
'post_status' => array('publish', 'private'),
'posts_per_page' => 8,
'meta_query' => array(
array(
'key' => 'end_date',
'compare' => '>=',
'value' => date('Y-m-d H:i:s'),
'type' => 'DATETIME'
)
),
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_key' => 'start_date',
'meta_type' => 'DATETIME',
);

Example Data:


I need to return the next sub_field that is in the future ONLY (Sept 10) in the first table cell below;


$post_query = new WP_Query($args);
if ($post_query->have_posts()) {
?>
<div class="events_widget">
<table> <?php
while ($post_query->have_posts()) {
$post_query->the_post();

$date = get_field("start_date");
$month = date("M", strtotime($date));
$day = date("d", strtotime($date));

?>

<tr>
<td width="100px"><span id="month"><?php echo $month;?></span></br><span id="day"><?php echo $day;?></span></td>
<td><a href="/events-schedule/<?php get_post_permalink( the_id() ); ?>"><?php the_field('location'); ?></a></td>
<td><a href="/events-schedule/<?php get_post_permalink( the_id() ); ?>"><?php the_field('event_title'); ?></a></td>
<td width="100px"><?php the_field('cost'); ?></td>
</tr>
<?php

}
?> </table>
</div><?php
}


My Answer:
To achieve this, you will need to modify your query to include a meta query for the repeater field. Here's an example of how you can modify your query to achieve this:

php
$date_now = date('Y-m-d H:i:s');
$time_now = strtotime($date_now);

$args = array(
'post_type' => array('events-schedule'),
'post_status' => array('publish', 'private'),
'posts_per_page' => -1, // Get all posts
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_key' => 'start_date',
'meta_type' => 'DATETIME',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'end_date',
'compare' => '>=',
'value' => date('Y-m-d H:i:s'),
'type' => 'DATETIME'
),
array(
'key' => 'repeater_field_name', // Replace 'repeater_field_name' with the actual field name of your repeater field
'value' => $date_now,
'compare' => '>=',
'type' => 'DATETIME'
)
)
);

$post_query = new WP_Query($args);

if ($post_query->have_posts()) {
?>


while ($post_query->have_posts()) {
$post_query->the_post();

// Get the repeater field values
$repeater_field = get_field('repeater_field_name'); // Replace 'repeater_field_name' with the actual field name of your repeater field
$next_date = '';

// Loop through the repeater field values to find the next upcoming date
foreach ($repeater_field as $row) {
$sub_field_date = $row['sub_field_name']; // Replace 'sub_field_name' with the actual sub field name of your DateTime field

if (strtotime($sub_field_date) > $time_now) {
$next_date = $sub_field_date;
break;
}
}

if (!empty($next_date)) {
$month = date("M", strtotime($next_date));
$day = date("d", strtotime($next_date));
?>







}
}
?>



}


Make sure to replace 'repeater_field_name' and 'sub_field_name' with the actual field names of your repeater field and sub field containing the DateTime value. This code will loop through all posts, find the next upcoming date in the repeater field, and display the relevant information in the table.

Rate this post

5 of 5 based on 3978 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us