Skip to content

Instantly share code, notes, and snippets.

@jchristopher
jchristopher / functions.php
Last active Jan 26, 2022
Automatically link to WooCommerce Product Variation detail when searching for a Product Variation SKU
View functions.php
<?php
// When using SearchWP it's necessary to disable WooCommerce's insistance on
// automatically redirecting to a single search result without showing the
// search results page, when that happens this hook doesn't run!
// Willing to bet this can be edited to accommodate, tips are welcome!
add_filter( 'woocommerce_redirect_single_search_result', '__return_false' );
function my_maybe_woocommerce_variation_permalink( $permalink ) {
if ( ! is_search() ) {
@jchristopher
jchristopher / gist:0cad8418fd9477c57b53
Last active Jan 18, 2022
Index WooCommerce product variation SKUs with the parent product
View gist:0cad8418fd9477c57b53
<?php
// index WooCommerce product_variation SKUs with the parent post
function my_searchwp_index_woocommerce_variation_skus( $extra_meta, $post_being_indexed ) {
// we only care about WooCommerce Products
if ( 'product' !== get_post_type( $post_being_indexed ) ) {
return $extra_meta;
}
@jchristopher
jchristopher / searchwp-customizations.php
Created Apr 27, 2021
Tell SearchWP to index both value and label from ACF Select field
View searchwp-customizations.php
<?php
// Tell SearchWP to index both value and label from ACF Select field.
add_filter( 'searchwp\source\post\attributes\meta', function( $meta_value, $args ) {
$acf_field_name = 'state'; // ACF Select field name.
if ( $acf_field_name !== substr( $args['meta_key'], strlen( $args['meta_key'] ) - strlen( $acf_field_name ) ) ) {
return $meta_value;
}
@jchristopher
jchristopher / searchwp-customizations.php
Created May 30, 2021
Tell SearchWP to exclude password protected results
View searchwp-customizations.php
<?php
// Tell SearchWP to exclude password protected results.
add_filter( 'searchwp\query\mods', function( $mods ) {
global $wpdb;
$mod = new \SearchWP\Mod();
$mod->set_local_table( $wpdb->posts );
$mod->on( 'ID', [ 'column' => 'id' ] );
$mod->raw_where_sql( function( $runtime ) {
@jchristopher
jchristopher / search-results.php
Last active Nov 18, 2021
Output SearchWP Live Ajax Search results grouped by Category taxonomy term
View search-results.php
<?php if ( ! have_posts() ) : ?>
<p>No results</p>
<?php endif; return; ?>
<?php
$grouped_results = [];
$no_term_results = [];
while ( have_posts() ) {
@jchristopher
jchristopher / searchwp-customizations.php
Last active Nov 17, 2021
Group SearchWP results by Source, sort by relevance within each Source group
View searchwp-customizations.php
<?php
// Group SearchWP results by Source, sort by relevance within each Source group.
add_filter( 'searchwp\query\mods', function( $mods, $query ) {
$mod = new \SearchWP\Mod();
$mod->order_by( function( $mod ) {
// Search results should be grouped by Sources in this order.
// NOTE: _ALL_ Engine Sources must be included here!
$source_order = [
'user',
View searchwp-customizations.php
<?php
// Randomize SearchWP search results.
add_filter( 'searchwp\query\mods', function( $mods ) {
$mod = new \SearchWP\Mod();
$mod->order_by( 'random', null, 1 );
$mods[] = $mod;
return $mods;
} );
View searchwp-customizations.php
<?php
// Limit SearchWP results to Posts and Pages.
add_filter( 'searchwp\query\mods', function( $mods, $query ) {
$mod = new \SearchWP\Mod();
$mod->set_where( [ [
'column' => 'source',
'value' => [
\SearchWP\Utils::get_post_type_source_name( 'post' ),
@jchristopher
jchristopher / functions.php
Created Nov 13, 2017
Allow SearchWP to index Draft posts and make them searchable in the WordPress Admin, not public
View functions.php
<?php
function my_searchwp_post_statuses( $post_status, $engine ) {
if ( is_admin() || did_action( 'searchwp_indexer_pre' ) ) {
$post_status = array( 'publish', 'draft' );
}
return $post_status;
}
@jchristopher
jchristopher / gist:512654fb6d7a3a666468
Last active Aug 2, 2021
Use a Shortcode to prevent SearchWP from indexing partial content, see https://searchwp.com/docs/kb/prevent-indexing-segments-content/
View gist:512654fb6d7a3a666468
<?php
// tell SearchWP to process shortcodes
add_filter( 'searchwp_do_shortcode', '__return_true' );
// if the SearchWP indexer is running prevent returning the
// content between [searchwp_no_index] and [/searchwp_no_index]
function shortcode_searchwp_no_index( $atts, $content = null ) {
// if the searchwp_indexer_running action fired, the indexer is running so don't return anything
return did_action( 'searchwp_indexer_running' ) ? '' : $content;