Home › Forums › Add-ons › Repeater Field › WP_Query by repeater date field
Hi guys,
I’m working on a course schedule at the moment and I’m stuck with querying posts by multiple dates with a repeater field.
I have a post called “Test course” and it contains a repeater called “course_data”.
There are 4 dates at the moment and the subfield is called “date”:
26 november 2015 (output: Ymd)
30 november 2015 (output: Ymd)
2 december 2015 (output: Ymd)
28 december 2015 (output: Ymd)
At the front-end there is only 1 post displayed and not 1 post for each date.
That should be:
26 november 2015 – Test course
30 november 2015 – Test course
2 december 2015 – Test course
28 december 2015 – Test course
What am I doing wrong?
The code:
<?php
// filter
function my_posts_where( $where ) {
$where = str_replace("meta_key = 'course_data_%", "meta_key LIKE 'course_data_%", $where);
return $where;
}
add_filter('posts_where', 'my_posts_where');
// find todays date
$date = date('Ymd');
// args
$args = array(
'numberposts' => -1,
'post_type' => 'post',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'course_data_%_date',
'compare' => '>=',
'value' => $date,
)
)
);
// query
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
<ul>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
Ok, I found a solution for the loop here: http://support.advancedcustomfields.com/forums/topic/sort-posts-by-date-picker-in-repeater-field/
But how do I integrate pagination after 10 items?
Hey Patrick,
You can try and use the following code for the pagination link.
<?php next_posts_link( 'Older Posts', $past_query->max_num_pages ); ?><?php previous_posts_link( 'Newer posts' ); ?>
This page should give you an idea about how to go about it: https://codex.wordpress.org/Function_Reference/next_posts_link#Usage_when_querying_the_loop_with_WP_Query.
I hope this helps.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
ACF wouldn’t be so widely used in WordPress if it didn’t have some pretty amazing capabilities. In this article, we look at a few of the features we’ll discuss during “7 things you didn’t know you could do with ACF” at #WPEDecode later this month. https://t.co/5lnsTxp81j pic.twitter.com/Yf0ThPG1QG
— Advanced Custom Fields (@wp_acf) March 16, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.