sql - add_filter function not working as expected -
i'm trying edit wp_query
in wordpress using add_filter
function. when var_dump
query request
outputs sql expected.
however, when runs returns error different query! know why query might change, , change much!
query request
var_dump
(as expected):
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 ) ) ) ) distance , lat latitude , lng longitude 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 mt1 on (wp_posts.id = mt1.post_id) inner join lat_lng_post on wp_posts.id = lat_lng_post.post_id 1=1 , ( wp_term_relationships.term_taxonomy_id in (2) ) , wp_posts.post_type = 'event' , ((wp_posts.post_status = 'publish')) , ( (wp_postmeta.meta_key 'date_%_start-date' , cast(wp_postmeta.meta_value signed) <= '20140704') , (mt1.meta_key 'date_%_end-date' , cast(mt1.meta_value signed) >= '20140627') ) , lat_lng_post.lat = lat , lat_lng_post.lng = lng , substr(wp_postmeta.meta_key, 1, 6) = substr(mt1.meta_key, 1, 6) group wp_posts.id having distance <= 20 order distance asc limit 0, 10
and query shows below error [unknown column 'lat' in 'field list']
(not expected):
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 ) ) ) ) distance , lat latitude , lng longitude wp_posts 1=1 , wp_posts.post_type = 'acf-field' , ((wp_posts.post_status = 'publish')) , wp_posts.post_name = 'field_535e6b9ffe3da' , lat_lng_post.lat = lat , lat_lng_post.lng = lng group wp_posts.id having distance <= 20 order distance asc limit 0, 1
note
i have custom table called lat_lng_post
has 3 columns, post_id
, lat
, lng
store location data each event (custom post type).
edit add_filter functions being used on query:
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 ) ) ) ) distance , lat latitude , lng 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 .= ' , lat_lng_post.lat = lat , lat_lng_post.lng = lng'; return $define; } add_filter('posts_where', 'lat_lng_define'); // having distance less 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 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 .= " , substr(wp_postmeta.meta_key, 1, 6) = substr(mt1.meta_key, 1, 6) "; return $where; } // fix setting date search field function date_to( $to ) { $to = str_replace("mt1.meta_key = 'date_%_end-date'", "mt1.meta_key 'date_%_end-date'", $to); return $to; } // fix setting date search field function date_from( $from ) { $from = str_replace("meta_key = 'date_%_start-date'", "meta_key 'date_%_start-date'", $from); return $from; } // fix ordering date function order_date( $like ) { $like = str_replace("mt2.meta_key = 'date_%_end-date'", "mt2.meta_key 'date_%_end-date'", $like); return $like; } // fix searching post title, requires characters match function title_filter( $where, &$wp_query ) { global $wpdb; if ( $search_term = $wp_query->get( 'title_like' ) ) { $where .= ' , ' . $wpdb->posts . '.post_title \'%' . esc_sql( like_escape( $search_term ) ) . '%\''; } return $where; }
also, wp_query
these functions being added to:
// add query title add_filter( 'posts_where', 'title_filter', 10, 2 ); // dates , 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'); // date inputs 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 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 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 );
it looks you're trying setup posts_*
filters secondary query, forget remove filters afterwards, affecting other queries run later.
i) can remove filter remove_filter()
function. here's example :
// add filter: add_filter( 'some_filter', 'some_filter_callback', $priority ); // run secondary query: $wp_query = new wp_query( $args ); // remove previous filter: remove_filter( 'some_filter', 'some_filter_callback', $priority );
where filter priority must match.
ii) way use add remove_filter()
inside some_filter_callback()
function. example:
add_filter( 'some_filter', 'some_filter_callback' ); $wp_query = new wp_query( $args );
where
some_filter_callback( $string ) { // remove current filter: remove_filter( current_filter(), __function__ ); // modifications input: // ... // output: return $string; }
this make sure filter run once.
iii) if you're trying modify main query, can restrict filter with:
if( ! is_admin() && is_main_query() ) { // ... }
or
if( ! is_admin() && $query->is_main_query() ) { // ... }
if you're using pre_get_posts
hook, $query
input argument.
iv) option create new class extends wp_query
, contains filters want:
class my_search_query extends wp_query { // ... }
where use
$query = new my_search_query( $args );
to run query.
Comments
Post a Comment