Support

Account

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

Solved

Inner join overrides Advanced Custom Fields plugin's get_field

  • I have this search that I made with inner join and where clause. I no longer get get_field() values, they all return false. How can I get ACF to work again? It must be something silly that I don’t know about.

    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;
                        });
    
                        add_filter( 'posts_where', function( $content ) use ( $minmax ){
                            $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_join', function(){
                            global $wpdb;
                            $join = " INNER JOIN wp_my_geodata AS geo ON ($wpdb->posts.ID = geo.post_id)";
                            return $join;
                        });
                    endif;
                }else{
                    return;
                }
            }
        }
    }
    
    add_filter( 'pre_get_posts', 'get_lat_long');

    The part that causes the issue is

    
                            add_filter( 'posts_where', function( $content ) use ( $minmax ){
                                $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_join', function(){
                                global $wpdb;
                                $join = " INNER JOIN wp_my_geodata AS geo ON ($wpdb->posts.ID = geo.post_id)";
                                return $join;
                            });

    I’m getting correct results, just not able to use get_field(‘field_name’); anymore.

  • Hi @perkunas

    I would think it’s because you add filters to every SQL query happening. You probably need to change the filter to actually call to a function doing this and then when you’re done use remove_filter to remove your custom joins.

  • The reasoning sounds right, any chance you could do a rough mock up that would point me in the right direction? Doesn’t have to be complete, just the logic behind it. I’m still unfamiliar with remove_filter and the join was a trial and error 😐

  • 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.

  • Thanks a million! I rewrote the functions, but you taught me the correct way of adding filters. I needed to remove the filters after use too.

  • No problem mate 🙂

    Glad I could help out!

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

The topic ‘Inner join overrides Advanced Custom Fields plugin's get_field’ is closed to new replies.