Support

Account

Forum Replies Created

  • Okay, I’ve asked my host to tell me (if this happens again) what was the responsible query.

    About the LIKE in the SQL, here was a solution : double the %

    $loc = '"what im looking for"';
    $posts_query = $wpdb->get_results("...... NOT LIKE '%%$loc%%' ....);

    Again, many thanks !! It works perfectly (see here it’s a temporal link while the site is under construction) 🙂

  • Thanks @hube2

    Sorry for the delay, I’ve had a big DB error for several days…

    I’ve tried to escape but it doesn’t work. Maybe with the prepare method, I’ll try to figure it out.

    Just before setting this topic on solve, I have one last question : what if this query takes too long and generate an error ? My website has been down for 4 days, and the host said it was due to a “never ending SQL request”…

    Is there a way to secure this query ?
    What I’ve done for now is to set a max for the $max_rows var (if it’s above 35 the loop won’t start).
    If you have any idea they are more than welcome 🙂

    Thanks a lot again for all your help on this

    Lucas

  • Hello @hube2

    Many thanks, I’ve tried this.
    I can see how close I am to the solution, but it’s just out of reach with the knowledges I have.
    It’s the first time I use the $wpdb method and I’m not sure what i am doing wrong (again !).

    So I dynamically make the SQL request to have something like this in a var:

    SELECT *
    FROM wp_posts
    INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
    INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
    INNER JOIN wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id)
    INNER JOIN wp_postmeta AS mt2 ON (wp_posts.ID = mt2.post_id)
    INNER JOIN wp_postmeta AS mt3 ON (wp_posts.ID = mt3.post_id)
    INNER JOIN wp_postmeta AS mt4 ON (wp_posts.ID = mt4.post_id)
    WHERE 1=1
      AND (((mt3.meta_key = 'tous_lots_0_surface_du_lot'
             AND CAST(mt3.meta_value AS SIGNED) BETWEEN '110.5' AND '155.25')
            AND (mt4.meta_key = 'tous_lots_0_etat_de_livraison'
                 AND CAST(mt4.meta_value AS CHAR) LIKE '%"1-loc"%'))
           OR ((mt3.meta_key = 'tous_lots_1_surface_du_lot'
                AND CAST(mt3.meta_value AS SIGNED) BETWEEN '110.5' AND '155.25')
               AND (mt4.meta_key = 'tous_lots_1_etat_de_livraison'
                    AND CAST(mt4.meta_value AS CHAR) LIKE '%"1-loc"%'))
           OR ((mt3.meta_key = 'tous_lots_2_surface_du_lot'
                AND CAST(mt3.meta_value AS SIGNED) BETWEEN '110.5' AND '155.25')
               AND (mt4.meta_key = 'tous_lots_2_etat_de_livraison'
                    AND CAST(mt4.meta_value AS CHAR) LIKE '%"1-loc"%'))
           OR (etc...)
      AND (wp_term_relationships.term_taxonomy_id IN (10))
      AND (wp_postmeta.meta_key = 'dept_nb_for_the_order'
           AND mt1.meta_key = 'city_name_for_the_order'
           AND mt2.meta_key = 'arrondissement')
      AND post_status = 'publish'
      AND wp_posts.post_type = 'post'
    GROUP BY wp_posts.ID
    ORDER BY CAST(wp_postmeta.meta_value AS CHAR) ASC, CAST(mt1.meta_value AS CHAR) ASC, CAST(mt2.meta_value AS CHAR) ASC LIMIT 0,
                                                                                                                                    10

    Then I send the query and loop through the results.
    It almost works !!!

    I think I just have a problem with LIKE ‘%”1-loc”%’
    Even if this string isn’t in the db, the result shows up.

    Thanks a lot for your help, I almost reach the end 🙂

  • 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

  • Hello, yes this helps me a lot !! I’ll give it a try asap.

    I just have a concern : what if the $max_rows is 10 (for example) and I have only 2 or 3 rows in most of my posts ?
    $repeater.’_’.$row.’_’$sufield2 would then point to something that doesn’t exist in the db, doesn’t it ?

    What if I insert the “for” loop in the “while” loop ? For each post it would make the right amount of loops and I reset the $max_posts at the end of the post loop.

    Thanks a lot, I will try to start with your idea 🙂

  • Yes I have, I am using the ‘my_post_where’ function in my code to replace the ‘=’ by ‘LIKE’.

    The problem is to search subfield by subfield, not “all the subfield in the post”.

    (I’ve edited my first)

  • Thank you John, it eventually worked, here is my working code :

    
    <?php
    function reverse_geocoding($form_data) {
    	
    	
    	$latlng = $latlng = $_POST['fields']['field_123abcd-this-is-my-map-field']['lat'].','.$_POST['fields']['field_123abcd-this-is-my-map-field']['lng'];
    	
    	// set your API key here
    	$server_key = '*****';
    	
    	// format this string with the appropriate latitude longitude
    	$url = 'https://maps.googleapis.com/maps/api/geocode/json?latlng='.$latlng.'&key='.$server_key.'&sensor=true&language=fr';
    	
    	// make the HTTP request
    	$data = @file_get_contents($url);
    	// parse the json response
    	
    	$jsondata = json_decode($data,true);
    	
    
    	if(is_array($jsondata )&& $jsondata ['status']=='OK') // this piece of code is looping through each component in ['address_components']
    	{
    		foreach ($jsondata['results'][0]['address_components'] as $comp) {
    			foreach ($comp['types'] as $currType){
    				// Here I get all the information I need and store it into vars
    				if($currType == 'administrative_area_level_2'){
    					$area_level_2 = $comp['long_name'];
    				}
    				if($currType == 'locality'){
    					$locality = $comp['long_name'];
    				}
    			}
    		}
    		
    	$coord = explode(",", $latlng);
    	
    	$loc_data = array('city' => $locality,
    					'area_level_2' => $area_level_2,
    					'lat' => $coord[0],
    					'lng' => $coord[1],
    					'zoom' => $zoom,
    					'address' => $form_data['field_556355e41efef']['address']);
    	}
    	
    	return $loc_data;
    	
    	
    }
    
    function acf_location_data( $post_id ) {
     
    		
    		$loc_data = reverse_geocoding($_POST['fields']); // I send all the
    
    		$_POST['fields']['field_123abcd-this-is-my-map-field'] = array(); // not sure if necessary, just to clean the data...
    		$_POST['fields']['field_123abcd-this-is-my-map-field'] = $loc_data; // insert all the data calculated in the funciton above
    		$_POST['fields']['field_456efgh-empty_hidden_field'] = $loc_data['area_level_2']; // here I insert just area_level_2 in order to filter / Order my SQL request later
    		
    }
    
    // run before ACF saves the $_POST['acf'] data
    add_action('acf/save_post', 'acf_location_data', 1);
    
    

    The update_field function wasn’t working, but step by step I’ve managed to do exactly want I was looking for. I hope this may be useful for someone out there.

    Thanks a lot John 🙂

    edit : it’s not very clean, but it works…. if I have time i will simplify it to give you a “generic” code 😉

  • And do you have any idea why it wouldn’t work ?
    I’ve simplified as much as possible

    function my_acf_save_post( $post_id ) {
        
      
    	update_field( "field_5593bd8134102", "This is a test!", $post_id );
    }
    
    // run before ACF saves the $_POST['acf'] data
    add_action('acf/save_post', 'my_acf_save_post', 1);

    Then I got on a post, fill all the field, and publish the post but… “This is a test” never appears.
    This code is used in the theme’s functions.php

    Is there any previous step I may have missed ?

  • Hello John,

    thank you for your answer, I’ve gave it a try, but it didn’t work… I don’t really understand why. Am I supposed to insert the code in my theme’s functions.php ?

    Then do something like this …? :

    <?php
    
    function my_acf_save_post( $post_id ) {
        
        // bail early if no ACF data
        if( empty($_POST['acf']) ) {
            
            return;
            
        }
        
          
        // do things with the var, insert the data i am interested in
     
        $_POST['acf']['field_abc123'] = $my_array_with_data_i_want
    
        
    }
    
    // run before ACF saves the $_POST['acf'] data
    add_action('acf/save_post', 'my_acf_save_post', 1);
    
    ?>

    Is this the spirit ?
    If yes it doesn’t work, and I can’t figure out why…

  • Hello,

    These ideas are exactly what I am working on currently. The dealerpower solution is great but in my case it doesn’t include all the information I need (for example I would like to save in the db the administrative_area_level_2 short & long)

    So maybe it would be better to let the choice to the user when we create the field.

    Thanks a lot for this plug-in, I hope it will never end its improvement 😉

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