I've used WP-Geo-Posts as a starting point for building a plugin which extends WooCommerce to support classifieds-like functionality. Using ACF I've associated products with a location (latitude and longitude) and I'm now building location support into search. Users provide search terms, a location and a radius. Results should be products within a certain radius, ordered by closest to the provided location. It's working perfectly, but the search is currently ignoring any search terms the user enters.
I've made a class which extends WP_Query to build the pseudo-field of 'distance'. You'd think I could just provide s
to the same $args
array, and it would scope by keyword like WP_Query does. Instead I get a blank page and the logs say
PHP Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 130968 bytes) in /home/mydir/home/mysite/public_html/wp-includes/query.php on line 1681, referer: http://example.com/shop/
I have this which works fine:
$args = array(
'post_type' => $query->get('post_type') ,
'posts_per_page' => 20,
'fields' => 'all',
'lat' => $_REQUEST['lat'],
'lng' => $_REQUEST['lng'],
'distance' => $_REQUEST['dist']
);
$query = new WP_Query_Geo( $args );
And this which leaks memory like gangbusters:
$args = array(
'post_type' => $query->get('post_type') ,
'posts_per_page' => 20,
'fields' => 'all',
'lat' => $_REQUEST['lat'],
'lng' => $_REQUEST['lng'],
'distance' => $_REQUEST['dist'],
's' => $search_terms
);
$query = new WP_Query_Geo( $args );
My class which extends WP_Query (WP_Query_Geo) uses concats (.=
) on $fields, $join and $where so I don't see why providing s
should do any harm.
This is the output of var_dump($query->request);
without the s
in $args:
string(799) "SELECT SQL_CALC_FOUND_ROWS wp_posts.*, ( 6371 * acos( cos( radians(48.4284207) ) * cos( radians( latitude.meta_value ) ) * cos( radians( longitude.meta_value ) - radians(-123.36564440000001) ) + sin( radians(48.4284207) ) * sin( radians( latitude.meta_value ) ) ) ) AS distance , latitude.meta_value AS latitude , longitude.meta_value AS longitude FROM wp_posts INNER JOIN wp_postmeta AS latitude ON wp_posts.ID = latitude.post_id INNER JOIN wp_postmeta AS longitude ON wp_posts.ID = longitude.post_id WHERE 1=1 AND wp_posts.post_type = 'product' AND (wp_posts.post_status = 'publish') AND latitude.meta_key="lat" AND longitude.meta_key="lng" HAVING distance <= 100 ORDER BY distance ASC, wp_posts.post_date DESC LIMIT 0, 20"
var_dump($query->request);
?$query->request
(or anything else) whens
is included because of the fatal error.pre_get_posts
and var_dumping$query->request
, I getNULL
.$query->request
on thepre_get_posts
hook. It is too soon.