Skip to content

Instantly share code, notes, and snippets.

Avatar

Jon Christopher jchristopher

View GitHub Profile
@jchristopher
jchristopher / searchwp-customizations.php
Created Oct 22, 2020
Sort SearchWP results in alphabetical order
View searchwp-customizations.php
<?php
// Sort SearchWP results in alphabetical order.
add_filter( 'searchwp\query\mods', function( $mods ) {
$mod = new \SearchWP\Mod( \SearchWP\Utils::get_post_type_source_name( 'post' ) );
$mod->order_by( function( $mod ) {
return $mod->get_local_table_alias() . '.post_title';
}, 'ASC', 1 );
$mods[] = $mod;
@jchristopher
jchristopher / searchwp-customizations.php
Created Oct 7, 2020
Customize SearchWP stopwords per Engine
View searchwp-customizations.php
<?php
/**
* Customize SearchWP stopwords per Engine.
*/
// Optional: remove all Stopwords so you can add only unique Stopwords per Engine.
add_filter( 'searchwp\stopwords', '__return_empty_array' );
// Add unique stopword(s) for a single SearchWP Engine.
@jchristopher
jchristopher / searchwp-customizations.php
Created Oct 6, 2020
Add unique stopword(s) for a single SearchWP Engine
View searchwp-customizations.php
<?php
// Add unique stopword(s) for a single SearchWP Engine.
add_filter( 'searchwp\query\search_string', function( $search_string, $query ) {
// Remove "apple" and "orange" for my_engine searches.
if ( 'my_engine' === $query->get_engine() ) {
$search_string = str_replace( [ 'apple', 'orange' ], '', $search_string );
}
return $search_string;
@jchristopher
jchristopher / searchwp-customizations.php
Last active Oct 3, 2020
Ranomize SearchWP 4 results (with pagination)
View searchwp-customizations.php
<?php
// In order to paginate randomized results, we need a seed.
add_action( 'init', function() {
if ( ! isset( $_COOKIE['seed'] ) ) {
setcookie( 'seed', rand(), 0, COOKIEPATH, COOKIE_DOMAIN );
}
} );
// Tell SearchWP to randomize the results based on the stored seed.
@jchristopher
jchristopher / ig.php
Created Oct 1, 2020
Store your recent Instagram posts as a WordPress transient. Simple. Might break. ¯\_(ツ)_/¯
View ig.php
<?php
// Super simple transient storage of the 4 most recent Instagramp posts.
// Stores only images/first image in carousel album.
// Assumes you already have an access token.
// Don't know if the access token invalidates over time. If it does, this doesn't handle it.
// Transient storage is an array of media objects directly from Instagram.
// Possible to sideload images into the Media library using https://developer.wordpress.org/reference/functions/media_sideload_image/
$transient_name = 'my_instagram_feed';
@jchristopher
jchristopher / searchwp-customizations.php
Last active Sep 30, 2020
Include WooCommerce Product Drafts in SearchWP Admin Searches
View searchwp-customizations.php
<?php
// Add Drafts to Products when indexing and searching in Admin.
add_filter( 'searchwp\post_stati\product', function( $stati ) {
$stati[] = 'draft';
$stati[] = 'private';
return $stati;
}, 30 );
@jchristopher
jchristopher / searchwp-customizations.php
Created Sep 29, 2020
Exclude email addresses from SearchWP's index
View searchwp-customizations.php
<?php
add_filter( 'searchwp\tokens', function( $tokens ) {
return array_filter( $tokens, function( $token ) {
return ! is_email( $token );
} );
}, 5 );
@jchristopher
jchristopher / searchwp-customizations.php
Created Sep 29, 2020
Add support for sorting by Name when searching WooCommerce Products in the Admin Area with SearchWP
View searchwp-customizations.php
<?php
add_filter( 'searchwp\query\mods', function( $mods ) {
if ( ! is_admin() ) {
return $mods;
}
if ( empty( $_GET['orderby'] || empty( $_GET['order'] ) ) {
return $mods;
}
@jchristopher
jchristopher / searchwp-customizations.php
Last active Sep 25, 2020
Index WooCommerce Product Variations with Products in SearchWP
View searchwp-customizations.php
<?php
// Add WooCommerce Product Variation data to parent Products in SearchWP.
add_filter( 'searchwp\entry\data', function( $data, \SearchWP\Entry $entry ) {
// If this is not a Product, there's nothing to do.
if ( 'product' !== get_post_type( $entry->get_id() ) ) {
return $data;
}
$my_extra_meta_key = 'searchwp_product_variations';
@jchristopher
jchristopher / searchwp-customizations.php
Last active Sep 18, 2020
Tell SearchWP to give more weight to WP_Posts with more comments
View searchwp-customizations.php
<?php
// Add relevance weight based on number of comments.
add_filter( 'searchwp\query\mods', function( $mods ) {
global $wpdb;
// Multiply the number of comments by this multiplier.
$comment_count_multiplier = 1;
$mod = new \SearchWP\Mod();
You can’t perform that action at this time.