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.

  • Hi

    The method is already working ?

    i tried.. and it dont work :/

    What i made :

    i got a custom field form, who search in ajax with get_users and with complexe request on many user’s customs fields

    all work fine, even filter taxonomy assigned to user like custom field

    but this dont work for my repeater field..

    the repeater key is : liste_des_langues & the item key : langue

    if add this :

    function wpza_replace_repeater_field( $where ) {
        $where = str_replace( "meta_key = 'liste_des_langues_$", "meta_key LIKE 'liste_des_langues_%", $where );
        return $where;
    }
    add_filter( 'posts_where', 'wpza_replace_repeater_field' );

    and this is my request args for this field :

    if($langues && $langues != 'all'){
            $temp = array(
                'key' =>  'liste_des_langues_$_langue',
                'value' =>  $langues,
                'compare' => 'LIKE',
            );
            array_push($request['meta_query'] , $temp);
        }

    this is the basic request :

    $request = array(
            'role__in' => array( 'consultant' ),
            'meta_query'      => array(),
            'tax_query'      => array(),
        );
    
        if(count($_GET) > 1){
            $request['meta_query']['relation'] = 'AND';
        }

    and i dont understand where is my mistake, someone can help please ?

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

The topic ‘wp_query and meta_query for repeater field values’ is closed to new replies.