Forums

Having difficulty with Adding a Filter to Search Results (1 post)

  1. discountufoparts
    Member
    Posted 7 months ago #

    I am trying to add this search filter to my search results but it just does not seem to have any effect. Can someone tell me how to go about adding it correctly. I have dumped variables and the filter is working correctly it just doesn't seem to run on the search results page for some reason.

    add_filter( 'pre_get_posts', 'cp_pre_get_search_posts' );
    function cp_pre_get_search_posts( $query ) {
    	global $wpdb;
    		$meta_query = array();
    		foreach ( $_POST as $key => $value ) {
    			if ( $value )
    			switch ( $key ) {
    				case 'cp_city_zipcode' :
    					$value = urlencode( $value );
    
    					$geocode = json_decode( wp_remote_retrieve_body( wp_remote_get( "http://maps.googleapis.com/maps/api/geocode/json?address=$value&sensor=false&region=us" ) ) );
    
    					if ( 'OK' == $geocode->status ) {
    						$lat = $geocode->results[0]->geometry->location->lat;
    						$lng = $geocode->results[0]->geometry->location->lng;
    						$rad = intval($_POST['distance']);
    
    						$R = 'mi' == 3959;
    						$table = 'wp_cp_ad_geocodes';
    						$results = $wpdb->get_results( $wpdb->prepare("SELECT post_id, (((acos(sin(($lat*pi()/180)) * sin((<code>lat</code>*pi()/180))+cos(($lat*pi()/180)) * cos((<code>lat</code>*pi()/180)) * cos((($lng- <code>lng</code>)*pi()/180))))*180/pi())*60*1.1515) AS distance FROM wp_cp_ad_geocodes HAVING distance < $rad ORDER BY distance"));
    
                                                    if (!$results) $post_ids[0] = -1; else $post_ids = array();
    						foreach( $results as $result )
    							$post_ids[] = $result->post_id;
    
    						$query->set( 'post__in', $post_ids );
    						print_r($post_ids);
    					} else {
    						// Google Maps API error
    					}
    					break;
    
    				case 'amount' :
    					$value = str_replace( array( get_option( 'cp_curr_symbol' ), ' ' ), '', $value );
    					$value = str_replace( ' ', '', $value );
    					$meta_query[] = array(
    								'key' => 'cp_price',
    								'value' => explode( '-', $value ),
    								'compare' => 'BETWEEN',
    								'type' => 'numeric',
    					);
    					break;
    
    				default :
    					if ( 'cp_' == substr( $key, 0, 3 ) ) {
    						$meta_query[] = array(
    							'key'   => $key,
    							'value' => $value,
    							'compare' => 'IN'
    						);
    					}
    					break;
    			}
    		}
    		return $query;
    		$query->set( 'meta_query', $meta_query );
    
    }

Reply

You must log in to post.

About this Topic

  • RSS feed for this topic
  • Started 7 months ago by discountufoparts
  • This topic is not resolved
  • WordPress version: 3.2.1