Support

Account

Home Forums Front-end Issues Inner join overrides Advanced Custom Fields plugin's get_field Reply To: Inner join overrides Advanced Custom Fields plugin's get_field

  • Actually rethinking it I think it’d be better to just have the filters completely separate and do the same if statements in them.. Something like this might work for you:

    
    <?php
    
    function my_custom_where( $where ){
    	
    	if ($query->is_search && isset($_GET['zipcode']) && isset($_GET['radius'])) {
    		
    		$where .= " AND ( ( (geo.lat BETWEEN '$minmax[min_latitude]' AND '$minmax[max_latitude]') AND (geo.lng BETWEEN '$minmax[min_longitude]' AND '$minmax[max_longitude]') )  )";
    		
    	}
    	
        return $where;
    }
    add_filter( 'posts_where', 'my_custom_where');
    
    function my_custom_join($join){
    	
    	if ($query->is_search && isset($_GET['zipcode']) && isset($_GET['radius'])) {
    		
    		global $wpdb;
    	    $join .= " INNER JOIN wp_my_geodata AS geo ON ($wpdb->posts.ID = geo.post_id)";
        
        }
        
        return $join;
    }
    add_filter( 'posts_join', 'my_custom_join');
    
    function get_lat_long($query)
    {
        if ( !is_admin() && $query->is_main_query() ) {
            if ($query->is_search) {
            $query->set('post_type', 'doctor');
            global $wpdb;
                if(isset($_GET['zipcode']) && isset($_GET['radius'])){
                    $zipcode = (int)$_GET['zipcode'];
                    $radius = (int)$_GET['radius'];
    
                    $lat_long = $wpdb->get_results("SELECT lat, lon FROM zips WHERE zip_code = '$zipcode' LIMIT 1");
                    if($lat_long):
                        global $minmax;
                        $minmax = bar_get_nearby( $lat_long[0]->lat, $lat_long[0]->lon, 0, $radius );
                        $minLat = $minmax['min_latitude'];
                        $maxLat = $minmax['max_latitude'];
                        $minLon = $minmax['min_longitude'];
                        $maxLon = $minmax['max_longitude'];
                        add_action( 'min_lat', function(){
                            echo $minLat;
                        });
    
                    endif;
                }else{
                    return;
                }
            }
        }
    }
    
    add_filter( 'pre_get_posts', 'get_lat_long');
             
    ?>
    

    Also note that in the code I do not overwrite the already existing where and joins but rather add to them.. It’s possible you’ll have to rewrite them to work as they should when ADDED rather than OVERWRITING the default SQL.