Support

Account

Home Forums Front-end Issues Font-end search form for a subfield Reply To: Font-end search form for a subfield

  • Okay so I’ve tried your code, it theoretically works!

    But the query takes very long and I eventually get an error (502 bad gateaway or error 500 internal server error)

    Here is my query (it is in an array and I use that array to make the WP_Query) :

    Array
    (
        [post_status] => publish
        [post_type] => post
        [category_name] => en_cours
        [meta_query] => Array
            (
                [dept_order] => Array
                    (
                        [key] => dept_nb_for_the_order
                        [compare] => EXISTS
                    )
    
                [city_order] => Array
                    (
                        [key] => city_name_for_the_order
                        [compare] => EXISTS
                    )
    
                [arrdt_order] => Array
                    (
                        [key] => arrondissement
                        [compare] => EXISTS
                    )
    
                [repeater] => Array
                    (
                        [relation] => OR
                        [0] => Array
                            (
                                [relation] => AND
                                [surface] => Array
                                    (
                                        [key] => tous_lots_0_surface_du_lot
                                        [value] => Array
                                            (
                                                [0] => 42.5
                                                [1] => 172.5
                                            )
    
                                        [type] => numeric
                                        [compare] => BETWEEN
                                    )
    
                                [date] => Array
                                    (
                                        [key] => tous_lots_0_date_de_livraison_du_lot
                                        [value] => 20190931
                                        [compare] => >=
                                        [type] => DATE
                                    )
    
                                [selection_cheminee] => Array
                                    (
                                        [key] => tous_lots_0_etat_de_livraison
                                        [value] => "6-cheminee"
                                        [compare] => LIKE
                                    )
    
                                [non_loue] => Array
                                    (
                                        [key] => tous_lots_0_etat_de_livraison
                                        [value] => "1-loc"
                                        [compare] => LIKE
                                    )
    
                            )
    
                        [1] => Array
                            (
                                [relation] => AND
                                [surface] => Array
                                    (
                                        [key] => tous_lots_1_surface_du_lot
                                        [value] => Array
                                            (
                                                [0] => 42.5
                                                [1] => 172.5
                                            )
    
                                        [type] => numeric
                                        [compare] => BETWEEN
                                    )
    
                                [date] => Array
                                    (
                                        [key] => tous_lots_1_date_de_livraison_du_lot
                                        [value] => 20190931
                                        [compare] => >=
                                        [type] => DATE
                                    )
    
                                [selection_cheminee] => Array
                                    (
                                        [key] => tous_lots_1_etat_de_livraison
                                        [value] => "6-cheminee"
                                        [compare] => LIKE
                                    )
    
                                [non_loue] => Array
                                    (
                                        [key] => tous_lots_1_etat_de_livraison
                                        [value] => "1-loc"
                                        [compare] => LIKE
                                    )
    
                            )
    
                        [2] => Array
                            (
                                [relation] => AND
                                [surface] => Array
                                    (
                                        [key] => tous_lots_2_surface_du_lot
                                        [value] => Array
                                            (
                                                [0] => 42.5
                                                [1] => 172.5
                                            )
    
                                        [type] => numeric
                                        [compare] => BETWEEN
                                    )
    
                                [date] => Array
                                    (
                                        [key] => tous_lots_2_date_de_livraison_du_lot
                                        [value] => 20190931
                                        [compare] => >=
                                        [type] => DATE
                                    )
    
                                [selection_cheminee] => Array
                                    (
                                        [key] => tous_lots_2_etat_de_livraison
                                        [value] => "6-cheminee"
                                        [compare] => LIKE
                                    )
    
                                [non_loue] => Array
                                    (
                                        [key] => tous_lots_2_etat_de_livraison
                                        [value] => "1-loc"
                                        [compare] => LIKE
                                    )
    
                            )
    
                        [3] => Array
                            (
                                [relation] => AND
                                [surface] => Array
                                    (
                                        [key] => tous_lots_3_surface_du_lot
                                        [value] => Array
                                            (
                                                [0] => 42.5
                                                [1] => 172.5
                                            )
    
                                        [type] => numeric
                                        [compare] => BETWEEN
                                    )
    
                                [date] => Array
                                    (
                                        [key] => tous_lots_3_date_de_livraison_du_lot
                                        [value] => 20190931
                                        [compare] => >=
                                        [type] => DATE
                                    )
    
                                [selection_cheminee] => Array
                                    (
                                        [key] => tous_lots_3_etat_de_livraison
                                        [value] => "6-cheminee"
                                        [compare] => LIKE
                                    )
    
                                [non_loue] => Array
                                    (
                                        [key] => tous_lots_3_etat_de_livraison
                                        [value] => "1-loc"
                                        [compare] => LIKE
                                    )
    
                            )
    
                    )
    
            )
    
        [orderby] => Array
            (
                [dept_order] => ASC
                [city_order] => ASC
                [arrdt_order] => ASC
            )
    
    )
    

    I know it’s long, and for now the $max_rows is just 4… it will be around 20 when all the data will be in the DB.

    Looking at the array, it seems it should work…

    Here is my PHP (added with my first post code):

    
    function replace_row($array, $row) {
    	if($array != '') {
    		$array = str_replace('_%_', '_'.$row.'_', $array);
    	}
    	return $array;
    }
    
    $repeater_query = array(relation => 'OR');
    		
    		while ($countposts->have_posts()) {
    			$countposts->the_post();
    			
    			 $count = intval(get_post_meta($post->ID, $repeater, true));
    
    			if ($count > $max_rows) {
    				$max_rows = $count;
    			}
    		}
    		
    		
    	
    		for ($row=0; $row<$max_rows; $row++) {
    			
    			$row_query = array('relation' => 'AND',
    							   'surface' => replace_row($surface, $row),
    							   'date' => replace_row($date, $row),
    							   'selection_cheminee' => replace_row($selection_cheminee, $row),
    							   'non_loue' => replace_row($non_loue, $row)
    								);
    			
    			$repeater_query[] = $row_query; 
    
    		}

    EDIT : here is a clue ->
    – when i set $max_rows = 1; i have no error (but the search only cover the 1st element of each repeater).
    – if i set it with the value “2” it no longer works… Despite the fact that each post has 2 or more element in the repeater field