Skip to content

Instantly share code, notes, and snippets.

View msaari's full-sized avatar

Mikko Saari msaari

View GitHub Profile
@msaari
msaari / relevanssi-date-query.php
Last active August 29, 2019 10:23
Natural-language date querying for Relevanssi
<?php
add_filter( 'relevanssi_modify_wp_query', 'rlv_date_query' );
function rlv_date_query( $query ) {
$time = strtotime( $query->query_vars['s'] );
if ( $time ) {
$year = date( 'Y', $time );
$use_year = strpos( $query->query_vars['s'], $year ) !== false ? true : false;
$month = date( 'm', $time );
$day = date( 'd', $time );
@msaari
msaari / search-results.php
Last active September 22, 2020 12:41
SearchWP Live Ajax Search template with support for users and taxonomies
<?php
/**
* Save this as searchwp-live-ajax-search/search-results.php inside your theme.
*/
?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); if ( 'draft' === $post->post_status ) continue; ?>
<?php $post_type = get_post_type_object( get_post_type() ); ?>
<div class="searchwp-live-search-result" role="option" id="" aria-selected="false">
@msaari
msaari / generatepress-mime-icon.php
Created April 5, 2019 04:12
PDF icons for GeneratePress
<?php
/**
* Add this to your theme functions.php (or preferably in a custom plugin, because a theme
* update will overwrite the functions.php.
*
* You can also try different locations by replacing the 'generate_before_entry_title' with
* one of the following:
*
* - generate_after_entry_title
@msaari
msaari / relevanssi-remove-duplicates.php
Created March 28, 2019 09:02
Remove duplicate postmeta
<?php
add_action( 'save_post', 'relevanssi_remove_duplicate_postmeta', 100 );
function relevanssi_remove_duplicate_postmeta( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
$pins = get_post_meta( $post_id, '_relevanssi_pin', false );
@msaari
msaari / relevanssi_index_embedded_pdf.php
Created March 22, 2019 04:09
Index PDFs embedded with pdfjs-viewer
<?php
add_filter( 'relevanssi_content_to_index', 'rlv_pdfjs_content', 10, 2 );
function rlv_pdfjs_content( $content, $post ) {
$m = preg_match_all( '/\[pdfjs-viewer url="(.*)"/', $post->post_content, $matches );
if ( $m ) {
global $wpdb;
$upload_dir = wp_upload_dir();
foreach ( $matches[1] as $pdf ) {
$pdf_url = ltrim( str_replace( $upload_dir['baseurl'], '', urldecode( $pdf ) ), '/' );
@msaari
msaari / relevanssi_multisite_fixes.php
Created March 21, 2019 08:00
Multisite fixes for Relevanssi and WP 5.1
<?php
// In /lib/install.php, replace the relevanssi_new_blog() function with this:
function relevanssi_new_blog( $blog ) {
if ( is_int( $blog ) ) {
$blog_id = $blog;
} else {
$blog_id = $blog->id;
}
@msaari
msaari / relevanssi-customfield-filter.php
Last active March 5, 2019 09:07
Filters out matches outside custom fields
<?php
add_filter( 'relevanssi_match', 'rlv_customfield_match_filter', 10 );
function rlv_customfield_match_filter( $match ) {
global $wp_query;
if ( 'listing' === $wp_query->query_vars['post_type'] ) {
// This is a listing search.
if ( $match->customfield < 1 ) {
// No custom fields were hit, set weight to zero.
$match->weight = 0;
@msaari
msaari / rlv_img_alt.php
Last active March 4, 2019 05:40
Pick up img tag title attribute from custom field values
<?php
// Add this to theme functions.php.
add_filter( 'relevanssi_custom_field_value', 'rlv_img_alt', 10 );
function rlv_img_alt( $value ) {
if ( ! is_array( $value ) ) {
$value = array( $value );
}
$new_values = array();
foreach ( $value as $value_string ) {
@msaari
msaari / acf_file_and_image.php
Created February 27, 2019 03:40
ACF image and file indexing
<?php
/**
* Add this to theme functions.php in order to index information about images and files
* linked to the post with the ACF image and file fields.
*
* Replace the field name with the correct name and choose which parts of the files and
* images to index.
*/
add_filter( 'relevanssi_content_to_index', 'rlv_acf_image_and_file', 10, 2 );
@msaari
msaari / relationship_fields.php
Created January 22, 2019 04:06
Flexible content relationship fields
<?php
add_filter( 'relevanssi_content_to_index', 'rlv_relationship_content', 10, 2 );
function rlv_relationship_content( $content, $post ) {
global $wpdb;
$relationships = unserialize( $wpdb->get_var( "SELECT meta_value FROM $wpdb->postmeta WHERE meta_key LIKE 'sidebar_groups_%_people'" ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions
if ( ! is_array( $relationships ) ) {
$relationships = array( $relationships );
}
foreach ( $relationships as $related_post_id ) {