我正在尝试使用add_filter
function在WordPress中编辑wp_query
。当我var_dump
我的查询request
它按预期输出SQL。add_filter函数未按预期工作
但是,它运行时会返回一个不同的查询错误!有谁知道为什么查询可能会改变,并且改变很多!
从request
var_dump
查询(如预期):
SELECT SQL_CALC_FOUND_ROWS wp_posts.*,
(3959 * acos(
cos(radians(52.486243))
* cos(radians(lat))
* cos(radians(lng) - radians(-1.890401))
+ sin(radians(52.486243))
* sin(radians(lat))
))
AS distance , lat AS latitude , lng AS longitude
FROM wp_posts
INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
INNER JOIN wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id)
INNER JOIN lat_lng_post ON wp_posts.ID = lat_lng_post.post_id
WHERE 1=1
AND (wp_term_relationships.term_taxonomy_id IN (2))
AND wp_posts.post_type = 'event'
AND ((wp_posts.post_status = 'publish'))
AND ((wp_postmeta.meta_key LIKE 'date_%_start-date' AND CAST(wp_postmeta.meta_value AS SIGNED) <= '20140704')
AND (mt1.meta_key LIKE 'date_%_end-date' AND CAST(mt1.meta_value AS SIGNED) >= '20140627'))
AND lat_lng_post.lat = lat
AND lat_lng_post.lng = lng
AND substr(wp_postmeta.meta_key, 1, 6) = substr(mt1.meta_key, 1, 6)
GROUP BY wp_posts.ID
HAVING distance <= 20
ORDER BY distance ASC
LIMIT 0, 10
这是显示错误[Unknown column 'lat' in 'field list']
(不预期)下面的查询:
SELECT wp_posts.*,
(3959 * acos(
cos(radians(52.486243))
* cos(radians(lat))
* cos(radians(lng) - radians(-1.890401))
+ sin(radians(52.486243))
* sin(radians(lat))
)) AS distance , lat AS latitude , lng AS longitude
FROM wp_posts
WHERE 1=1
AND wp_posts.post_type = 'acf-field'
AND ((wp_posts.post_status = 'publish'))
AND wp_posts.post_name = 'field_535e6b9ffe3da'
AND lat_lng_post.lat = lat
AND lat_lng_post.lng = lng
GROUP BY wp_posts.ID
HAVING distance <= 20
ORDER BY distance ASC
LIMIT 0, 1
注意
我有一个名为lat_lng_post
的自定义表,它有三列,post_id
,lat
,lng
来存储每个事件的位置数据(自定义帖子类型)。在查询中使用
编辑所有功能的add_filter:
function distance_query($distance) {
$lat = $_SESSION['search']['lat'];
$lng = $_SESSION['search']['long'];
$distance .= ",
(3959 * acos(
cos(radians(".$lat."))
* cos(radians(lat))
* cos(radians(lng) - radians(".$lng."))
+ sin(radians(".$lat."))
* sin(radians(lat))
))
AS distance , lat AS latitude , lng AS longitude";
return $distance;
}
add_filter('posts_fields', 'distance_query');
// add lat_lng_post table inner join
function lat_lng_join($join) {
$join = str_replace('(wp_posts.ID = mt1.post_id)', '(wp_posts.ID = mt1.post_id) INNER JOIN lat_lng_post ON wp_posts.ID = lat_lng_post.post_id', $join);
return $join;
}
add_filter('posts_join', 'lat_lng_join');
// set lat lng definition
function lat_lng_define($define) {
$define .= ' AND lat_lng_post.lat = lat AND lat_lng_post.lng = lng';
return $define;
}
add_filter('posts_where', 'lat_lng_define');
// HAVING distance less than user distance
function having_distance($having) {
$radius = $_SESSION['search']['distance'];
$having = 'wp_posts.ID HAVING distance <= '.$radius.'';
return $having;
}
add_filter('posts_groupby', 'having_distance');
// if sorting by distance
function sort_distance($sortdistance) {
$sortdistance = 'distance ASC';
return $sortdistance;
}
if($_SESSION['search']['sort-by'] == 'distance') :
add_filter('posts_orderby', 'sort_distance');
endif;
function add_additional_where_condition($where) {
$where .= " AND substr(wp_postmeta.meta_key, 1, 6) = substr(mt1.meta_key, 1, 6) ";
return $where;
}
// fix for setting the date to search field
function date_to($to) {
$to = str_replace("mt1.meta_key = 'date_%_end-date'", "mt1.meta_key LIKE 'date_%_end-date'", $to);
return $to;
}
// fix for setting the date from search field
function date_from($from) {
$from = str_replace("meta_key = 'date_%_start-date'", "meta_key LIKE 'date_%_start-date'", $from);
return $from;
}
// fix for ordering by date
function order_date($like) {
$like = str_replace("mt2.meta_key = 'date_%_end-date'", "mt2.meta_key LIKE 'date_%_end-date'", $like);
return $like;
}
// fix for searching by LIKE post title, requires all characters to match
function title_filter($where, &$wp_query) {
global $wpdb;
if ($search_term = $wp_query->get('title_like')) {
$where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'%' . esc_sql(like_escape($search_term)) . '%\'';
}
return $where;
}
而且,这是wp_query
本身,这些功能被添加到:
// add query for title
add_filter('posts_where', 'title_filter', 10, 2);
// dates to and from logic
add_filter('posts_where', 'date_from');
add_filter('posts_where', 'date_to');
add_filter('posts_where', 'order_date');
add_filter('posts_where', 'add_additional_where_condition');
// get date inputs from search
$date1 = str_replace('/', '-', $_SESSION['search']['from']);
$when = date("Ymd", strtotime($date1));
$date2 = str_replace('/', '-', $_SESSION['search']['to']);
$when2 = date("Ymd", strtotime($date2));
$year = date('Y');
// Declare the query arguments
if (get_query_var('paged')) {
$paged = get_query_var('paged');
} else if (get_query_var('page')) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
// make keywords an array
$keywordString = $_SESSION['search']['keyword'];
$keywords = explode(', ', $keywordString);
$taxQuery = array(
'relation' => 'AND',
array (
'taxonomy' => 'main-cat',
'field' => 'slug',
'terms' => $_SESSION['search']['cat']
)
);
if($_SESSION['search']['keyword'] != '') {
$taxQuery[] = array(
'taxonomy' => 'sub-cat',
'field' => 'name',
'terms' => $keywords
);
}
$args = array(
// general
'post_type' => 'event',
'post_status' => 'publish',
'posts_per_page' => 10,
'paged' => $paged,
'cache_results' => false,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
'meta_key' => $_SESSION['search']['sort-key'],
'orderby' => $_SESSION['search']['sort-by'],
'order' => 'ASC',
// category filter
'tax_query' => $taxQuery,
// date filter
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'date_%_start-date',
'value' => $when2,
'compare' => '<=',
'type' => 'NUMERIC'
),
array (
'key' => 'date_%_end-date',
'value' => $when,
'compare' => '>=',
'type' => 'NUMERIC'
)
)
);
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query($args);
我想你试图修改多个单一查询的SQL。你能分享过滤器代码吗?它是主要查询还是辅助查询,您正在尝试修改? – birgire
嗨,这是唯一一个在页面上运行的查询,它基本上是一个搜索页面,带有永久链接'/ event-search /',将使用所有'add_filter'函数编辑我的问题。 – lukeseager
这些过滤器会影响所有'WP_Query'查询,除了'get_posts()'包装器,其中的过滤器被抑制。 – birgire