Home Forums ACF PRO % in query on repeater


% in query on repeater

  • Hi.
    im trying to verify multiple conditions in repeater field.
    For exemple : There is multiple value for a Tires (With, height, diam). All of them are in a repeater field.

    Based on the turn around about the esc_sql thinks in wordpress, i did this :

    function my_posts_where( $where ) {
        global $wpdb;
        $where = str_replace(
                  "meta_key = 'dimensions_%", 
                  "meta_key LIKE 'dimensions_%",
        return $where;
    add_filter('posts_where', 'my_posts_where');					
    	$args = array(
        'post_type'  => 'pneu',
        'meta_query' => array (
    		array (
    		'relation' => 'AND', 
    				'key'     => 'dimensions_%_pneu_av_largeur',
    				'value'   => $id_largeur_pneu,
    				'compare' => '=',
    				'key'     => 'dimensions_%_pneu_av_hauteur',
    				'value'   => $id_hauteur_pneu,
    				'compare' => '=',
    				'key'     => 'dimensions_%_pneu_av_diametre',
    				'value'   => $id_diametre_pneu,
    				'compare' => '=',

    The problem is im not able to have a value based on the row. Actually if the 3 value are found in a column whatever the row of the repeater, it’s displaying a result.
    Exemple :

    | HEIGHT | WITH | DIAM |
    | 100 | 90 | 17 |
    | 110 | 90 | 17 |
    | 120 | 80 | 18 |

    If i query 100,80,17 i got a result because based on % im not able to control the entire row

    Is there is a way to query the result based on a single row ?

    Thank you.

  • The answer is that you can’t. There isn’t any way to query based on values on the same row. I would go another route.

    I recently published this article

    In a case like yours I would concatenate the values of each row into a standard WP meta field and then I would query by the concatenated values.

  • Thank you for your respond John.

    So, based on your advised, i did create an hidden field to put an array with my value in it, wish im planning to query after. Here it is.

    Any hint to query this array after ?

    // Insérer les dimension des pneus dans une meta array. 
    function my_acf_save_post( $post_id ) {
    	$post_type = get_post_type($post_id);
    		if ($post_type != 'pneu') {
    	if( have_rows('dimensions_av', $post_id) ):
    	while( have_rows('dimensions_av') ): the_row();
    		$largeur_pneu = get_term( get_sub_field('pneu_av_largeur'), 'largeur_pneu' ); 
    		$hauteur_pneu = get_term( get_sub_field('pneu_av_hauteur'), 'hauteur_pneu' ); 
    		$diametre_pneu = get_term( get_sub_field('pneu_av_diametre'), 'diametre_pneu' ); 
    		$size_pneu[] = $largeur_pneu->name.'/'.$hauteur_pneu->name.'-'.$diametre_pneu->name; 
    		//echo $size_pneu;
    	if( have_rows('dimensions_ar', $post_id) ):
    	while( have_rows('dimensions_ar') ): the_row();
    		$largeur_pneu = get_term( get_sub_field('pneu_ar_largeur'), 'largeur_pneu' ); 
    		$hauteur_pneu = get_term( get_sub_field('pneu_ar_hauteur'), 'hauteur_pneu' ); 
    		$diametre_pneu = get_term( get_sub_field('pneu_ar_diametre'), 'diametre_pneu' ); 
    		$size_pneu[] = $largeur_pneu->name.'/'.$hauteur_pneu->name.'-'.$diametre_pneu->name; 
    		//echo $size_pneu;
    	update_field('tailles_pneu', $size_pneu ,$post_id);
    // run before ACF saves the $_POST['acf'] data
    add_action('acf/save_post', 'my_acf_save_post', 10);
  • I think that you missed the important point of my article. Make sure you read the code example and the comments in the example.

    You’re using update_field() which is updating an acf field and the new field is outside of ACF and added using standard WP function

    You’re also updating the field with an array and you can’t do a query based on an array stored in the database.

    The idea is that you get each row of the repeater. Concatenate something into a string, for example 100/90/17 and then save each of these strings using add_post_meta(). Then when you query the posts you do a meta query on this new meta_key and the value you query by is the concatenated a similarly concatenated string.

    This will be more difficult in your case because you’re using a taxonomy for these values so you’ll need to get the string values you want to concatenate from the terms. But the idea is the same. You need to have unique values stored into a standard WP custom field that you can query on.

  • Thank you for the respond.

    Actually i did solve the issue by storing an array in a meta and query the array using compare ‘LIKE’ with wp_query metaquery wish is working fine as im looking for specifics concatenate value/string.

    It’s working. Is there is any contradictions to use this method ?

  • ‘LIKE’ are slow. Any query that searches the meta_value column is slow because it is not indexed. ‘LIKE’ queries on the meta_value field are even slower. This may or may not have a large impact on site performance, it really depends on how many of these queries you are doing.

  • Thank you for your support. I will make some teste of performance once finish.
    Making this topic solve, and thank you again John for your support.

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

The topic ‘% in query on repeater’ is closed to new replies.