Support

Account

Home Forums Add-ons Repeater Field WP_Query by repeater date field

Solving

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.

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘WP_Query by repeater date field’ is closed to new replies.