Support

Account

Home Forums Add-ons Repeater Field Search engine with repeater field and wp_query

Solving

Search engine with repeater field and wp_query

  • Hello everyone.

    I’m developing a real estate website with a search engine based on 6 filters.

    There is a content type “Programme” where there are several types of habitations. So I have a repeater “prix” with 2 sub fields “typo_prog” and “prix_prog”. It’s a table with this kind of rows :
    T3 | 290000
    T4 | 310000
    T5 | 350000

    The first field (T3, T4, T5…) is the typology and the second field is the price.

    In my search engine, I have a filter “Maximum price” and another “Typology”. But with my request, if I enter 310000 and T5, there is a result whereas it shouldn’t because the T5 is more expensive than 310000.

    How can I filter the wp_query ?

    Here is my code (I removed the useless filters for my question) :

    $prix = !empty($_POST['prix']) ? $_POST['prix'] : 999999999;
    
    if(!empty($_POST)) {
    
        function recherche_typo_prix($where){
            $where = str_replace("meta_key = 'prix_$", "meta_key LIKE 'prix_%", $where);
            return $where;
        }
        add_filter('posts_where', 'recherche_typo_prix');
    
        $args = array(
            'numberposts'   => -1,
            'post_type'     => array('programme'),
            'post_status'   => 'publish',
            'orderby'       => 'date',
            'fields'        => 'ids',
            'order'         => 'DESC',
            'meta_query'    => [
                'relation'      => 'AND',
              
                array(
                    'key' => 'prix_$_typo_prog',
                    'value' => $_POST['typologie'],
                    'compare' => '=',
                ),
                array(
                    'key' => 'prix_$_prix_prog',
                    'value' => $prix,
                    'type' => 'NUMERIC',
                    'compare' => '<='
                ),
            
    
            ],
        );
        $produits = new WP_Query($args);
    }
    

    Thanks a lot for your help !

  • There isn’t any way, using a query, to associate rows, unless you do 2 or more queries.

    First you need to get the posts that have one of them, for example, get all the posts with “T5”. Then you need to get the row index and then do another query for the other field using this row index. Also, you cannot do this second query on more than a single index, so if “T5” does not occupy the same row index on all posts you’ll need to do multiple queries here.

    Basically, what you’re trying to do is pretty much impossible using repeaters.

  • Ok. I understand.

    I’ll keep trying. Thanks.

  • Ok. I understand.

    I’ll keep trying. Thanks.

  • Thinking about it, it might be possible, but it could cause a performance issue.

    If you have a limited number of rows in the DB for this. In my example let’s say that there are no more than 2 rows in the repeater

    
    $meta_query = array(
      'relation' => 'OR',
      // first potential row
      array(
        'relation' => 'AND',
        array(
          'key' => 'prix_0_typo_prog',
          'value' => $_POST['typologie'],
          'compare' => '=',
        ),
        array(
          'key' => 'prix_0_prix_prog',
          'value' => $prix,
          'type' => 'NUMERIC',
          'compare' => '<='
        )
      ),
      // second potential row
      array(
        'relation' => 'AND',
        array(
          'key' => 'prix_1_typo_prog',
          'value' => $_POST['typologie'],
          'compare' => '=',
        ),
        array(
          'key' => 'prix_1_prix_prog',
          'value' => $prix,
          'type' => 'NUMERIC',
          'compare' => '<='
        )
      ),
      // continue for every potential row
    );
    

    But if you have too many rows and too many nested queries it will eventually time out.

  • There will be around 2000 “Programmes” in the DB…

    I’ve found another way to do it. Maybe not the best way or the cleanest but hey, it works !

    <?php foreach($produits->posts as $id):?>
       <?php if( have_rows('prix',$id)): ?>
    
          <?php /*If Typology AND Price are selected in the search engine */ ?>
          <?php while( have_rows('prix',$id) ) : the_row();?>
              <?php if((!empty($_POST['typologie']) && $_POST['typologie'] == get_sub_field('typo_prog')) && (!empty($_POST['prix']) && $_POST['prix'] > get_sub_field('prix_prog'))):?>
    
                     <a href="<?php the_permalink($id);?>" class="produit col-md-4">
                        MY CONTENT
                     </a> 
               <?php endif;?>
            <?php endwhile;?>
    
       <?php /*If Typology or price are NOT selected in the search engine */ ?>
        <?php if(empty($_POST['typologie']) || empty($_POST['prix'])):?>
    
           <a href="<?php the_permalink($id);?>" class="produit col-md-4">
              MY CONTENT
           </a>
        <?php endif;?>
    
      <?php  endif;?>
    <?php endforeach;?>
  • Ok, the part that I asked help for is working, but not everything, so don’t take my code above as my final anwser :p

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

You must be logged in to reply to this topic.