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 / gist:3135783eb3521bebbc5e
Last active Oct 20, 2020
Disable the default SearchWP Live Search results theme CSS while retaining the positioning
View gist:3135783eb3521bebbc5e
<?php
function my_remove_searchwp_live_search_theme_css() {
wp_dequeue_style( 'searchwp-live-search' );
}
add_action( 'wp_enqueue_scripts', 'my_remove_searchwp_live_search_theme_css', 20 );
@jchristopher
jchristopher / functions.php
Last active Oct 16, 2020
Use Elementor to power SearchWP Supplemental Engine
View functions.php
<?php
// @link https://searchwp.com/v3/docs/kb/using-elementor-for-supplemental-engines/
// We need to flag the search form.
add_action( 'elementor_pro/search_form/after_input', function( $form ) {
// Check to see if this is the right Search Form.
$settings = $form->get_data( 'settings' );
@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 / functions.php
Last active Oct 1, 2020
Change the WordPress search query parameter from 's' to something else
View functions.php
<?php
add_filter('init', function(){
global $wp;
$wp->add_query_var( 'search_query' );
$wp->remove_query_var( 's' );
} );
@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 );
You can’t perform that action at this time.