Support

Account

Home Forums General Issues wp_query and meta_query for repeater field values

Solving

wp_query and meta_query for repeater field values

  • I’ve got a wp_query that queries posts under a custom post type Events, and uses meta_query to filter posts by values of a repeater field, to filter events that are in the future or past. The events are set up with a repeater for dates and times. The query used to work, but according to ACF’s documentation (https://www.advancedcustomfields.com/resources/query-posts-custom-fields/ looking specifically at “4. Sub custom field values”), as of WordPress 4.8.3 the code needed to be changed in order to work. I replaced the % in the query args with $ characters, but had no luck. I tried also adding the filter that it says you now need, replacing their “location” with my custom field of “dates_repeater” but I end up with a broken page and 500 error. Below is my code. Without the filter taken from the documentation, it brings up no results, with it, a 500 error.

    // filter
    function my_posts_where( $where ) {
    
    	$where = str_replace("meta_key = 'dates_repeater_$", "meta_key LIKE 'dates_repeater_%", $where);
    
    	return $where;
    }
    
    add_filter('posts_where', 'my_posts_where');
    
    $today = current_time('Ymd');
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array(
    	'post_type' => 'event',
    	'post_status' => 'publish',
    	'posts_per_page' => '8',
    	'meta_query' => array(
    		'relation'		=> 'AND',
    		array(
    			'key' => 'dates_repeater_$_event_date',
    			'compare' => '>=',
    			'value' => $today,
    		),
    		array(
    			'key' => 'event_type',
    			'compare' => '=',
    			'value' => 'biocom',
    		),
    	),
    	'meta_key' => 'dates_repeater_$_event_date',
    	'orderby' => 'meta_value',
    	'order' => 'ASC',
    	'paged' => $paged,
    );
  • Hi,

    I know this is a old post but, did you find a solution ?
    I have exactly the same problem and I don’t know how to fix it.

    I know that we can’t do an order by on a repeater field, but . Im looking for how to do a meta query on a repeater field.

    There is my code :

    $args = array(
    							'post_type' => 'product',
    							'post_status' =>  'publish',
    							'meta_query'	=> array(
    								'relation'		=> 'OR',
    								array(
    									'key'		=> 'date_formation_$_date_session',
    									'value'		=> '20191108',
    									'type' 		=> 'DATE',
    									'compare'	=> '='
    								),
    								array(
    									'key'		=> 'date_formation_$_date_session',
    									'value'		=> '20191208',
    									'type' 		=> 'DATE',
    									'compare'	=> '='
    								),
    							)
    						);
  • Hi,
    I was on the same problem today and I find a solution. I paste it here with your variables names :

    
    // filter
    function my_posts_where( $where ) {
    	$where = str_replace("meta_key = 'date_formation_$", "meta_key LIKE 'date_formation_%", $where);
    	return $where;
    }
    add_filter('posts_where', 'my_posts_where');
    
    // find todays date
    $date = date('Ymd');
    
    // args
    $args = array(
    	'numberposts'	=> -1,
    	'post_type'		=> 'product',
    	'meta_query'	=> array(
    		'relation'		=> 'OR',
    		array(
    			'key'		=> 'date_formation_$_date_session',
    			'compare'	=> '>=',
    			'value'		=> $date,
    		),
    		array(
    			'key'		=> 'date_formation_$_date_session',
    			'compare'	=> '>=',
    			'value'		=> $date,
    		)
    	)
    );
    $the_query = new WP_Query( $args );
    

    And then, I work on my results for ordering.

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

You must be logged in to reply to this topic.