WordPress.org

Forums

Custom filter query with meta_values that is in an array? (3 posts)

  1. jesperb
    Member
    Posted 1 year ago #

    Hey,

    Been staring at this too long so I figured I'd post here to see if I'd be able to get a push in the right direction.

    The rundown:
    I'm trying to limit the regular WP_Query to only return posts within a certain radius of the user. The post's got a "location"-field (advanced custom field f.y.i). And I'm saving the users' latitude and longitude as a cookie.

    However, the posts' locaiton is saved in an array:

    Array (
    [address] => DAL 221, 830 60 Föllinge, Sverige
    [lat] => 63.68805485609542
    [lng] => 14.087100587500004
    )

    How the heck do I select them separately this way? Am I even on the right way here?

    What I've got so far:

    function filter_where_custom() {
    
                        $latsearch = $_COOKIE["lat"]; //user latitude
                        $lngsearch = $_COOKIE["long"]; //user longitude
                        $distance = 2; //set radius of 2km
                        $lng = '?????'; //post longitude
                        $lat = '?????'; //post latitude
    
                        $where .= "
                        AND
                        (6371 * acos( cos( deg2rad($latsearch) ) * cos( deg2rad( $lat ) ) * cos( deg2rad( $lng ) - deg2rad($lngsearch) ) + sin( deg2rad($latsearch) ) * sin( deg2rad( $lat ) ) ) ) < $distance
                        ";
                        return $where;
                    }

    Hope my question isnt too vague, thanks for the help!

  2. jesperb
    Member
    Posted 1 year ago #

    Realized I'm making it more complicated than it is. I've now separated the longitude and latitude into two separate meta fields. Still trying to figure out how to select them though.

  3. jesperb
    Member
    Posted 1 year ago #

    So, I've gotten a bit further, stuck again, this returns no results. What am I doing wrong?

    function filter_where_custom() {
    
            $latsearch = $_COOKIE["lat"]; //user latitude
            $lngsearch = $_COOKIE["long"]; //user longitude
    
            if((isset($_COOKIE["omrade"])) && ($_COOKIE["omrade"] == 'norrtalje')){
                $distance = 10; //set radius of 10km
            }else if(isset($_COOKIE["omrade"])){
                $distance = $_COOKIE["omrade"]; //set radius of user defined
            }else{
                $distance = 2; //set radius of 2km
            }
    
            global $wpdb;
    
            $latitude = $wpdb->get_col("SELECT * FROM $wpdb->postmeta WHERE meta_key = 'latitude'", 3);
            $longitude = $wpdb->get_col("SELECT * FROM $wpdb->postmeta WHERE meta_key = 'longitude'", 3);
            $lat = $latitude[0];
            $lng = $longitude[0];
    
            $where .= " AND '" . (6371 * acos( cos( deg2rad($latsearch) ) * cos( deg2rad( $lat ) ) * cos( deg2rad( $lng ) - deg2rad($lngsearch) ) + sin( deg2rad($latsearch) ) * sin( deg2rad( $lat ) ) ) ) < $distance . "'";
    
            return $where;
    }

    I'm applying the query like so:

    $today = date('ymd');
        $args = array(
            'post_type' => 'att-ata',
            'tax_query' => array(
    			'taxonomy' => 'typer',
    			'field' => 'slug',
    			'terms' => 'lunch'
    		),
            'posts_per_page' => -1,
    	    'meta_key' => 'lunch_datum',
            'meta_query' => array(
                array(
                    'key' => 'lunch_datum',
                    'value' => date('ymd'),
                    'compare' => '=',
                    'type' => 'DATE'
                )
            ),
    	    'orderby' => 'meta_value',
    	    'order' => 'ASC',
        );
        add_filter('posts_where', 'filter_where_custom');
        $lunchquery = new WP_Query($args);
        remove_filter('posts_where', 'filter_where_custom');

Topic Closed

This topic has been closed to new replies.

About this Topic