Skip to content

Instantly share code, notes, and snippets.

View msaari's full-sized avatar

Mikko Saari msaari

View GitHub Profile
@msaari
msaari / flatsome-post-page.php
Last active October 9, 2018 10:59
Make Flatsome post and page search work with Relevanssi
<?php
// Add custom Theme Functions here
remove_action( 'woocommerce_after_main_content', 'flatsome_pages_in_search_results', 10 );
// Add Pages and blog posts to top of search results if set.
function relevanssi_pages_in_search_results() {
if ( ! is_search() || ! get_theme_mod( 'search_result', 1 ) ) {
return;
}
@msaari
msaari / indexing-check.php
Created September 13, 2018 09:33
Relevanssi indexing test
<?php
// Add this to theme functions.php, then go save the problem post and let me know what this prints out.
add_filter( 'relevanssi_post_content_before_tokenize', 'rlv_test_content' );
function rlv_test_content( $content ) {
var_dump( $content );
exit();
}
@msaari
msaari / custom_field_boost.php
Created September 13, 2018 03:46
Add boost to exact matches in custom fields
<?php
add_filter( 'relevanssi_results', 'rlv_exact_boost' );
function rlv_exact_boost( $results ) {
$query = strtolower( get_search_query() );
foreach ( $results as $post_id => $weight ) {
$custom_fields = get_post_meta( $post_id );
foreach ( $custom_fields as $field => $values ) {
if ( in_array( $field, array( 'custom_field_1', 'custom_field_2', 'custom_field_3' ), true ) ) {
$values = implode( ' ', $values );
@msaari
msaari / search.php
Created September 6, 2018 03:05
Search.php with better multisite excerpts
<?php
/**
* /lib/search.php
*
* @package Relevanssi
* @author Mikko Saari
* @license https://wordpress.org/about/gpl/ GNU General Public License
* @see https://www.relevanssi.com/
*/
@msaari
msaari / relevanssi-attachment-titles.php
Created September 3, 2018 05:55
Index attachment titles for parent
<?php
// Add this function to your theme functions.php and reindex.
add_filter( 'relevanssi_content_to_index', 'rlv_index_attachment_names', 10, 2 );
function rlv_index_attachment_names( $content, $post_id ) {
$attachments = get_children(
array(
'post_parent' => $post_id,
'post_type' => 'attachment',
)
@msaari
msaari / bento-content.php
Created August 21, 2018 03:12
Bento post content
<?php
// Add this to yor theme functions.php
function bento_post_content() {
global $post;
// If project post type and has a sidebar, exit
if ( get_post_type() == 'project' && get_post_meta( $post->ID, 'bento_sidebar_layout', true ) != 'full-width' ) {
return;
}
@msaari
msaari / json.json
Created August 20, 2018 08:19
JSON-esimerkki
[
[
{
"url":"https:\/\/www.example.ca\/",
"title":"Canadian page",
"lang":"en_ca"
},
{
"url":"http:\/\/www.example.com\/",
"title":"US page",
@msaari
msaari / relevanssi-div-filter.php
Created July 17, 2018 03:25
Relevanssi div filter
<?php
// Add this to the theme functions.php
add_filter( 'relevanssi_post_content', 'rlv_div_remove' );
function rlv_div_remove( $content ) {
$content = preg_replace( '/<div.*/s', '', $content );
return $content;
}
@msaari
msaari / woocommerce-filter-nav.php
Created July 6, 2018 03:18
Fix for WooCommerce Filter by attribute
<?php
// Add this function to your theme functions.php
add_filter( 'woocommerce_get_filtered_term_product_counts_query', 'rlv_attribute_filter' );
function rlv_attribute_filter( $query ) {
global $wp_query, $wpdb, $relevanssi_variables;
$query['from'] .= ' INNER JOIN ' . $relevanssi_variables['relevanssi_table'] . ' ON ' . $wpdb->posts . '.ID = ' . $relevanssi_variables['relevanssi_table'] . '.doc';
$split_where = explode( 'AND', $query['where'] );
$count = count( $split_where );
@msaari
msaari / relevanssi-lib-common.php
Created May 10, 2018 02:57
Correct Simple Membership support
<?php
// Replace the relevanssi_default_post_ok() function in the file lib/common.php with this:
/**
* Checks whether the user is allowed to see the post.
*
* The default behaviour on 'relevanssi_post_ok' filter hook. Do note that while
* this function takes $post_ok as a parameter, it actually doesn't care much
* about the previous value, and will instead overwrite it. If you want to make
* sure your value is preserved, either disable this default function, or run