Support

Account

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

  • 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…