Continue reading Only return exact title matches if possible

Only return exact title matches if possible

We have posts with titles like “A perfect match”. If a user searches for this exact title, only this post should show up in the results. Otherwise, the search should run as default. There are several approaches to this, but the best is relevanssi_hits_filter. This function goes through all the posts, checks if the post…

Read more Only return exact title matches if possible 0 Comment on Only return exact title matches if possible
Continue reading FileBird: Filtering attachments by folder

FileBird: Filtering attachments by folder

FileBird is one of many WordPress plugins that provide more control over the attachments in the Media Library. You can use the FileBird folders in Relevanssi searches to filter attachments based on their folder. Here’s a simple function you can add to your site: Once you add this, you can use the folder parameter to…

Read more FileBird: Filtering attachments by folder 0 Comment on FileBird: Filtering attachments by folder
Continue reading WPML: Replacing posts with translations

WPML: Replacing posts with translations

…the translated posts. You get a wider range of results, but all in the current language. This solution works best when all your posts appear in all languages. To use this, add this code to your site: remove_filter( ‘relevanssi_hits_filter’, ‘relevanssi_wpml_filter’ ); // Remove the original WPML filter. add_filter( ‘relevanssi_hits_filter’, ‘relevanssi_wpml_filter_show_translation’……); // Replace with our new filter. function relevanssi_wpml_filter_show_translation( $data ) { $wpml_post_type_setting = apply_filters( ‘wpml_setting’, false, ‘custom_posts_sync_option’ ); $current_blog_language = get_bloginfo( ‘language’ ); $filtered_hits = array(); foreach ( $data[0] as $hit ) { $original_hit = $hit; $object_array = relevanssi_get_an_object( $hit ); $hit = $object_array[‘object’]; $format = $object_array[‘format’]; if (……is a post in a translated post type. if ( intval( $hit->ID ) === intval( $id ) ) { // The post is in the current language. if ( ! in_array( $id, wp_list_pluck( $filtered_hits, ‘ID’ ), false ) ) { $filtered_hits[] = $original_hit; } } elseif ( intval( $hit->ID )…

Read more WPML: Replacing posts with translations 0 Comment on WPML: Replacing posts with translations
Continue reading Searching for all descendants of a page

Searching for all descendants of a page

…$ancestors = get_post_ancestors( $hit ); if ( in_array( intval( $wp_query->query_vars[‘parent’] ), $ancestors, true ) ) { // One of the lower level descendants. $clan[] = $hit; } } } // Only include the filtered posts. $hits[0] = $clan; return $hits; } This solution works well in smaller databases (“small” meaning…

Read more Searching for all descendants of a page 2 Comments on Searching for all descendants of a page
Continue reading WooCommerce: Return only exact matches for SKU searches

WooCommerce: Return only exact matches for SKU searches

This little filter function works on relevanssi_hits_filter. When a search query is made that matches an SKU (or any other custom field, but SKUs are the most likely scenario here), only results that match the SKU will be returned. For this to work, Relevanssi must be set to index the _sku custom field (because otherwise,…

Read more WooCommerce: Return only exact matches for SKU searches 1 Comment on WooCommerce: Return only exact matches for SKU searches
Continue reading Wishlist Member: Sorting the posts by membership levels

Wishlist Member: Sorting the posts by membership levels

…} if ( empty( $rlv_wishlist_level_posts[ $level ] ) ) { $results = wlmapi_get_level_posts( $level ); $rlv_wishlist_level_posts[ $level ] = array_fill_keys( array_column( $results[‘posts‘][‘post‘], ‘ID’ ), true ); } return $rlv_wishlist_level_posts[ $level ]; } /** * Filter function that returns true if posts is on the specific level. * * @param WP_Post…The post object to check. * * @return boolean True if post is on the specific level. */ function rlv_free_filter( $post ) { $free_posts = rlv_generate_level_post_list( 1313127748 ); return isset( $free_posts[ $post->ID ] ); } /** * Comparison function for array_udiff: post objects are considered * equal if they have……that posts on the specific Wishlist Member level * are first in the results. * * @param array $hits Contains the posts in $hits[0]. * * @return array The $hits array, filtered. */ function rlv_free_posts_first( $hits ) { $free_posts = array_filter( $hits[0], ‘rlv_free_filter’ ); $other_posts = array_udiff( $hits[0], $free_posts, ‘rlv_id_comparison’…

Read more Wishlist Member: Sorting the posts by membership levels 0 Comment on Wishlist Member: Sorting the posts by membership levels
Continue reading Showing only one recurring event

Showing only one recurring event

Some event calendar plugins do recurring events by creating many posts. That’s fine until those cloned posts fill up your search results. This function will only show one of each post with the same title and will take the one with the first date. The date is checked from _EventStartDate custom field, but it’s easy…

Read more Showing only one recurring event 2 Comments on Showing only one recurring event
Continue reading Category filter for search results pages

Category filter for search results pages

…included in the search results. So, here’s a two-part function that fixes that. First, we need a filter that processes the search results and gets a list of categories in the results. This filter runs very late on the relevanssi_hits_filter hook, so that it runs after other filters on the……in place, there’s instead a link that removes all category filters. function rlv_category_dropdown() { global $rlv_categories_present, $wp_query; if ( ! empty( $wp_query->query_vars[‘cat’] ) ) { $url = esc_url( remove_query_arg( ‘cat’ ) ); echo “<p><a href=’$url’>Remove category filter</a>.</p>”; } else { $select = “<select id=’rlv_cat’ name=’rlv_cat’><option value=”>Choose a category</option>”; foreach (……same hook: add_filter( ‘relevanssi_hits_filter’, ‘rlv_gather_categories’, 99 ); function rlv_gather_categories( $hits ) { global $rlv_categories_present; $rlv_categories_present = array(); foreach ( $hits[0] as $hit ) { $terms = get_the_terms( $hit->ID, ‘category’ ); if ( is_array( $terms ) ) { foreach ( $terms as $term ) { $rlv_categories_present[ $term->term_id ] = $term->name; }…

Read more Category filter for search results pages 33 Comments on Category filter for search results pages