• Resolved krishnathapa

    (@krishnathapa)


    Hi,

    I have created a custom taxonomy ‘location_tag’ and also added on custom_sql like this.

    	if (isset($_GET['filter']) && $_GET['filter']) {
    		$filter_ids = array_map('absint', explode(',', $_GET['filter']));
    		$cat_filter = "INNER JOIN $wpdb->term_relationships AS term_rel ON posts.ID = term_rel.object_id
    									 INNER JOIN $wpdb->term_taxonomy AS term_tax ON term_rel.term_taxonomy_id = term_tax.term_taxonomy_id
    													AND term_tax.taxonomy = 'wpsl_store_category'
    													AND term_tax.term_id IN (" . implode(',', $filter_ids) . ")";
    	} else {
    		$cat_filter = '';
    	}
    
    	// Get location tag ID
    	if (isset($_GET['tag_filter']) && $_GET['tag_filter']) {
    		$filter_tagids = array_map('absint', explode(',', $_GET['tag_filter']));
    		$tag_filter = "INNER JOIN $wpdb->term_relationships AS term_rel ON posts.ID = term_rel.object_id
    									 INNER JOIN $wpdb->term_taxonomy AS term_tax ON term_rel.term_taxonomy_id = term_tax.term_taxonomy_id
    													AND term_tax.taxonomy = 'location_tag'
    													AND term_tax.term_id IN (" . implode(',', $filter_tagids) . ")";
    	} else {
    		$tag_filter = '';
    	}
    
    	// The default query that selects store location that fall within the selected radius. 
    	$sql = "SELECT post_lat.meta_value AS lat,
                       post_lng.meta_value AS lng,
                       posts.ID, 
                       ( %d * acos( cos( radians( %s ) ) * cos( radians( post_lat.meta_value ) ) * cos( radians( post_lng.meta_value ) - radians( %s ) ) + sin( radians( %s ) ) * sin( radians( post_lat.meta_value ) ) ) ) 
                    AS distance        
                  FROM $wpdb->posts AS posts 
            INNER JOIN $wpdb->postmeta AS post_lat ON post_lat.post_id = posts.ID AND post_lat.meta_key = 'wpsl_lat'
            INNER JOIN $wpdb->postmeta AS post_lng ON post_lng.post_id = posts.ID AND post_lng.meta_key = 'wpsl_lng'
    				$cat_filter
    				$tag_filter
                 WHERE posts.post_type = 'wpsl_stores' AND posts.post_status = 'publish' $sql_sort";
    
    	return $sql;
    }

    It works fine when I search either using default categories or filter using custom taxonomy I created. But it gives no result when I select option from both and search.
    I attached screenshot if it helps
    https://tinyurl.com/2ecacofn

    Thank you

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi!

    I think this is a duplicate of a support ticket you sent us, I replied you there, so we’ll keep the conversation there as well if you don’t mind ??

    Regards,

    Thread Starter krishnathapa

    (@krishnathapa)

    Sure,

    Thank you for reply.

    Thread Starter krishnathapa

    (@krishnathapa)

    Thank you for your help. My issue is resolved by changing these codes.

    // Check if we need to filter the results by category.
    	if (isset($args['filter']) && $args['filter']) {
    		$filter_ids = array_map('absint', explode(',', $args['filter']));
    		$cat_filter = "INNER JOIN $wpdb->term_relationships AS term_rel1 ON posts.ID = term_rel1.object_id
    									 INNER JOIN $wpdb->term_taxonomy AS term_tax1 ON term_rel1.term_taxonomy_id = term_tax1.term_taxonomy_id
    													AND term_tax1.taxonomy = 'wpsl_store_category'
    													AND term_tax1.term_id IN (" . implode(',', $filter_ids) . ")";
    	} else {
    		$cat_filter = '';
    	}
    
    	// Get location tag ID
    	if (isset($args['tag_filter']) && $args['tag_filter']) {
    		$filter_tagids = array_map('absint', explode(',', $args['tag_filter']));
    		$tag_filter = "INNER JOIN $wpdb->term_relationships AS term_rel2 ON posts.ID = term_rel2.object_id
    									 INNER JOIN $wpdb->term_taxonomy AS term_tax2 ON term_rel2.term_taxonomy_id = term_tax2.term_taxonomy_id
    													AND term_tax2.taxonomy = 'location_tag'
    													AND term_tax2.term_id IN (" . implode(',', $filter_tagids) . ")";
    	} else {
    		$tag_filter = '';
    	}

    I was using multiple INNER JOIN with the same aliases. I changed term_rel to term_rel1 & term_rel2 and term_tax to term_tax1 & term_tax2 which solved my issues.

    Best
    Krishna

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Custom search filter not working’ is closed to new replies.