Support

Account

Home Forums Add-ons Repeater Field How do I query a repeater which uses recurring dates?

Solving

How do I query a repeater which uses recurring dates?

  • I was wondering how I can do lookups on a repeater which uses recurring dates for my events.

    Example Dates

    13/05/2015 – 14/05/2015
    16/05/2051 – 24/05/2015

    I have tried something like the following bit of code but it does not return the correct results where the start date is in range but the end date is outside.

    $args = array (
    ‘post_type’ => ‘post’,
    ‘meta_query’ => array(
    array(
    ‘key’ => ‘start_date’,
    ‘compare’ => ‘<=’,
    ‘value’ => $today,
    ),
    array(
    ‘key’ => ‘end_date’,
    ‘compare’ => ‘>=’,
    ‘value’ => $today,
    )
    ),
    );

    Thanks in advance

  • are you sure about the compare?
    it should only show dates that are currently ongoing (have a startdate today or inside the past, and a enddate of today or the future.)

    you could do it after query with php with code like that:

    <?php 
    $today = date('Ymd');
    $start_date = get_field('start_date');
    $end_date = get_field('end_date');
    if ((strtotime($today) >= strtotime($start_date)) && (strtotime($today) <= strtotime($end_date))) { //echo or something you like to do with the dates
    }
    ?>
    

    else, if you still need/wish to work with meta_value i can help you only with this info:
    date of datepicker inside DB is always build like 20150513 year month day without spaces, no matter what input or output format you give the date field with acf settings for that field.
    i am not sure if your $today has the same format/value. hope that help you to solve your problem.

    of your example dates it could only show the first (even if you would correct the 2051 to 2015)

  • Thanks for your reply. Sorry that was a typo in the supplied dates

    Example Dates

    13/05/2015 – 14/05/2015
    16/05/2015 – 24/05/2015

    I have been doing some test using the following ranges

    TESTS
    1 = 13/05/2015 – 14/05/2015 – Should return results
    2 = 15/05/2015 – 15/05/2015 – Should not return results
    3 = 18/05/2015 – 22/05/2015 – Should return results
    4 = 18/05/2015 – 25/05/2015 – Should return results (because the search start date is after the event start date)

    Test 4 is causing problems.

  • This reply has been marked as private.
  • i cant see private posts/replys.
    would it make a difference if you change query (add relation line)?

    'meta_query' => array(
    		'relation' => 'OR',
    array(

    and i begin to not understand what dates you like to show & if you have one or more input field or if you use today to check if date should be displayed or not

  • Thanks but it looks like its the query that gets generated thats the problem, could be an issue with the JOIN. I think its gonna have to be recoded/build again

    Thanks

  • I have been testing with the following events and dates…

    Test 1

    23/05/2015 – 23/05/2015
    25/05/2015 – 29/05/2015

    Test 2

    24/05/2015 – 24/05/2015

    The trouble is that the following code is returning Test 1 and Test 2 for the date range of 24/05/2015 – 24/05/2015 when only Test 2 should be returned

    <?php
    
    		// filter
    		function my_posts_where( $where ) {
    	
    			$where = str_replace("meta_key = 'event_dates_%", "meta_key LIKE 'event_dates_%", $where);
    	
    			return $where;
    		}
    	
    		add_filter('posts_where', 'my_posts_where');
    
    		$args = array (
    		    'post_type' => 'event',
    		    'meta_query' => array(
    			    'relation'		=> 'AND',
    				array(
    			        'key'		=> 'event_dates_%_start_date',
    			        'compare'	=> '<=',
    			        'value'		=> '20150524',
    			    ),
    			     array(
    			        'key'		=> 'event_dates_%_end_date',
    			        'compare'	=> '>=',
    			        'value'		=> '20150524',
    			    )
    		    ),
    		);
    
    		$the_query = new WP_Query( $args );
    
    ?>
  • probably it is because:
    event_dates_0_start_date AND event_dates_1_end_date accomplish the start before testdate and end after testdate.

  • Hi @mediawerk

    Have you received my email last week? I’m looking forward to hearing from you.

    Sorry to interrupt the thread

    Thanks
    E

  • Hello,
    Did you find a solution? Having similar trouble with this.
    ..
    Just found the solution for my problem with date fields inside a repeater field.
    Maybe this will help somebody else too:

    'meta_query' => array(
                      array(
                       'key'     => $repeater_date . '_%_date_time',
    		           'value'   => array( $min_date, $max_date ),
    		           'compare' => 'BETWEEN',
    		           'type'    => 'DATE',
    		 )
    )
  • Hi @britta , please, can you post your complete solution to @b1naryb0y original question ? I can’t understand what you made to get working.

    Me, I had to take an alternative approach.

    The following code will never work for repeater fields:

    $args = array (
    		    'post_type' => 'event',
    		    'meta_query' => array(
    			    'relation'		=> 'AND',
    				array(
    			        'key'		=> 'event_dates_%_start_date',
    			        'compare'	=> '<=',
    			        'value'		=> '20150524',
    			    ),
    			     array(
    			        'key'		=> 'event_dates_%_end_date',
    			        'compare'	=> '>=',
    			        'value'		=> '20150524',
    			    )
    		    ),
    		);

    As suggested by @mediawerk , event_dates_0_start_date AND event_dates_1_end_date accomplish the start before testdate and end after testdate.

    So I replaced it with:

    array(  
                  'key' => 'opening_dates',
                  'value' => 20150524,
                  'compare' => 'LIKE'
                 ),
    			

    The 'opening_dates' postmeta field contains all the single dates resulting from the repeating “start_date"/"end_date" couple and it was created with this code of mine placed in functions.php using the "acf/save_post" hook:

    
    	function create_opening_dates_array_when_saving_post_with_repeating_start_end_dates($post_id) {
    	
    	
    		// bail early the post type to apply
    		// src: https://support.advancedcustomfields.com/forums/topic/acfsave_post-action/
    		$post_type = get_post_type($post_id);
    			if ($post_type != 'event' ) {
    				return;
    			}
    			
    			
    			// not used here
    			$fields = $_POST['acf']; 
    			
    			// $intervals will contain the interval couples: ['single_interval']=> array("20170301", "20170320")
    			$intervals = array();
    			
    			
    			// 
    			// loop repeater fields
    			if( have_rows('event_dates') ):
    			
    			 	// loop through the rows of data
    			    while ( have_rows('event_dates') ) : the_row();
    			
    			       // for each row, get the "from" "to" couple and save it in the "single_interval" key (array) in the intervals array
    			       $from =  get_sub_field('start_date', false, false);
    			       $to =  get_sub_field('end_date', false, false);
    			       $intervals[]['single_interval'] = array($from, $to);
    
    			
    			    endwhile;
    			
    			else :
    			
    			    // no rows found
    			
    			endif;
    			 
    		// here will be saved the opening date vales	
    		$final_array = array();
    
    		 // loop the array containing the couples of intervals
    		 foreach($intervals as $single_interval) {
    			 
    			 // $intervals = array("20170301", "20170320")
    			 foreach($single_interval as $intervals) {
    			 	
    			 	// fill in missing in-between dates...
    				$arrays_of_single_dates = getDatesFromRange($intervals[0], $intervals[1]);
    				
    				// loop the resulting array and save each single value in the final array...
    				foreach($arrays_of_single_dates as $single) {
    					$final_array[] = intval($single); // from string to integer
    				}
    			}
    		
    		};
    	  // var_dump($final_array);
    	  
    	  
    	  // create or update a meta_key field called "opening_dates" and set as meta_value the $final_array containing all the opening dates
    	  update_post_meta( $post_id, 'opening_dates', $final_array);
    
    	 // var_dump($final_array);	  
    	};
    
    	
    	add_action('acf/save_post', 'create_opening_dates_array_when_saving_post_with_repeating_start_end_dates', 1);
    

    I’ve got the getDatesFromRange function from here.

    I wonder if @Elliot knows a simpler and better performing solution anyway…

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

The topic ‘How do I query a repeater which uses recurring dates?’ is closed to new replies.