Skip to content

Instantly share code, notes, and snippets.

View msaari's full-sized avatar

Mikko Saari msaari

View GitHub Profile
@msaari
msaari / relevanssi-category-filter.php
Created November 14, 2017 07:11
Relevanssi category filter buttons
<?php
// In theme functions.php, add this:
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' );
@msaari
msaari / relevanssi-create-excerpt.php
Created November 9, 2017 10:54
Improved excerpt creation
<?php
// Replace the relevanssi_create_excerpt() function in lib/excerpts-highlights.php with this:
function relevanssi_create_excerpt($content, $terms, $query) {
// If you need to modify these on the go, use 'pre_option_relevanssi_excerpt_length' filter.
$excerpt_length = get_option("relevanssi_excerpt_length");
$type = get_option("relevanssi_excerpt_type");
$best_excerpt_term_hits = -1;
@msaari
msaari / relevanssi-ajax.php
Created November 7, 2017 04:37
Relevanssi AJAX search
<?php
add_action('wp_ajax_relevanssi_search', 'rlv_ajax_search');
add_action('wp_ajax_nopriv_relevanssi_search', 'rlv_ajax_search');
function rlv_ajax_search() {
$query = new WP_Query();
$query->set('s', $_POST['s']);
/* Any other parameters you want to set, for example:
$query->set('post_types', 'page');
$query->set('numberposts', 100);
@msaari
msaari / relevanssi-simple-membership.php
Created October 26, 2017 03:40
Relevanssi support for Simple Membership
<?php
// Replace the relevanssi_default_post_ok() function in lib/common.php with this to add support for Simple Membership
function relevanssi_default_post_ok($post_ok, $doc) {
$status = relevanssi_get_post_status($doc);
// if it's not public, don't show
if ('publish' != $status) {
$post_ok = false;
@msaari
msaari / relevanssi-log.sql
Created October 24, 2017 04:34
Relevanssi log table SQL
CREATE TABLE `wp_relevanssi_log` (
`id` bigint(9) NOT NULL AUTO_INCREMENT,
`query` varchar(200) COLLATE utf8mb4_unicode_ci NOT NULL,
`hits` mediumint(9) NOT NULL DEFAULT '0',
`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`user_id` bigint(20) NOT NULL DEFAULT '0',
`ip` varchar(40) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
@msaari
msaari / simple-german-stemmer.php
Last active April 13, 2018 09:31
Simple German stemmer for Relevanssi
<?php
/* Simple German stemmer
A simple suffix stripper that can be used to stem German texts.
*/
add_filter( 'relevanssi_stemmer', 'relevanssi_simple_german_stemmer' );
function relevanssi_simple_german_stemmer( $term ) {
$term = str_replace( 'ß', 'ss', $term );
$len = strlen( $term );
$s_endings = array( 'b', 'd', 'f', 'g', 'h', 'k', 'l', 'm', 'n', 'r', 't' );
@msaari
msaari / relevanssi-include-children.php
Created September 26, 2017 03:36
Set "include_children" to false for "product_cat" taxonomy queries
<?php
// Add this to theme functions.php
add_filter('relevanssi_modify_wp_query', 'rlv_modify_query');
function rlv_modify_query($query) {
for ($i = 0; $i < count($query->tax_query->queries); $i++) {
if (isset($query->tax_query->queries[$i])) {
$row = $query->tax_query->queries[$i];
if (isset($row['taxonomy']) && $row['taxonomy'] == "product_cat") {
@msaari
msaari / relevanssi-block-faq.php
Created September 25, 2017 17:06
Remove [ultimate-faqs] shortcode
<?php
// Add this to theme functions.php
add_filter('relevanssi_pre_excerpt_content', 'rlv_remove_faq');
function rlv_remove_faq($content) {
$content = preg_replace('/\[ultimate-faqs.*?\]/', '', $content);
return $content;
}
@msaari
msaari / relevanssi-groups.php
Created September 8, 2017 05:38
Groups support for Relevanssi
<?php
// Add this to theme functions.php
add_filter('relevanssi_post_ok', 'rlv_groups_post_ok', 10, 2);
function rlv_groups_post_ok($post_ok, $doc) {
$user = wp_get_current_user();
$access = Groups_Post_Access::user_can_read_post($doc, $user->ID);
return $access;
}
@msaari
msaari / relevanssi-tax-term-fields.php
Last active September 15, 2017 17:08
Adding ACF fields for taxonomy terms
<?php
// Add this to theme functions.php and adjust the field name (and copy the lines as many times as
// necessary to include all the fields you want).
add_filter('relevanssi_tax_term_additional_content', 'rlv_index_term_fields', 10, 2);
function rlv_index_term_fields($content, $term) {
$field = get_field('taxonomyslug_' . $term->term_id, 'field_name');
$content .= " " . $field;
return $content;