Quantcast
Channel: Topic Tag: MySQL | WordPress.org
Viewing all articles
Browse latest Browse all 5530

jesperb on "Custom filter query with meta_values that is in an array?"

$
0
0

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');

Viewing all articles
Browse latest Browse all 5530

Trending Articles