Skip to content

Instantly share code, notes, and snippets.

View searchwp-customizations.php
<?php
// Allow searching by WP_Post ID.
function swp14237_searchwp_results( $results, $attributes ) {
$search_query = get_search_query();
if ( ! is_numeric( $search_query ) ) {
return $results;
}
@jchristopher
jchristopher / searchwp-customizations.php
Last active Apr 1, 2021
Arbitrary SearchWP weight multiplier on date as Custom Field value
View searchwp-customizations.php
<?php
// Modify SearchWP calculated relevance using multiplier.
// @link https://searchwp.com/documentation/knowledge-base/add-relevance-weight-date/
class My_SearchWP_Date_Modifier {
private $post_type = 'post';
private $meta_key = 'event_date';
private $modifier_past = 0.5;
private $modifier_future = 1.5;
private $alias = 'myswpdm';
@jchristopher
jchristopher / searchwp-customizations.php
Created Mar 26, 2021
searchwp\auto_update_providers hook example
View searchwp-customizations.php
<?php
// Tell SearchWP to automatically update its providers when switching sites.
// @link https://searchwp.com/documentation/hooks/searchwp-auto_update_providers/
add_filter( 'searchwp\auto_update_providers', '__return_true' );
// Retrieve results from this site.
$searchwp_site_1 = new \SWP_Query( [
's' => 'coffee',
] );
@jchristopher
jchristopher / searchwp-customizations.php
Last active Mar 31, 2021
Enfold theme live search integration with SearchWP (version 4 and version 3)
View searchwp-customizations.php
<?php
// **********************************************************************//
// Enfold Search WP integration */
// **********************************************************************//
add_filter( 'avf_ajax_search_function', 'avia_init_searchwp', 10, 4 );
function avia_init_searchwp( $function_name, $search_query, $search_parameters, $defaults ) {
$function_name = class_exists( 'SWP_Query' ) ? 'avia_searchwp_search' : $function_name;
return $function_name;
@jchristopher
jchristopher / searchwp-customizations.php
Created Mar 16, 2021
Enable full wpDataTables table Shortcode parsing when SearchWP indexer is running
View searchwp-customizations.php
<?php
// wpDataTables compatibility with SearchWP. Retrieve entire data table
// if the indexer is running (regardless of wpDataTables settings) to ensure
// all wpDataTables Shortcode content is indexed when parsing Shortcodes.
add_filter( 'wpdatatables_filter_table_metadata', function( $data, $table_id ) {
if ( did_action( 'searchwp\indexer\batch' ) ) {
$data->pagination = false;
$data->showAllRows = true;
$data->server_side = false;
@jchristopher
jchristopher / searchwp-customizations.php
Created Mar 11, 2021
Integrate SearchWP with JetSmartFilters search using JetEngine Listing Grid to display results
View searchwp-customizations.php
<?php
// Integrate SearchWP with JetSmartFilters search using
// JetEngine Listing Grid to display results.
add_action( 'pre_get_posts', function( $wp_query ) {
if (
! isset( $wp_query->query['jet_smart_filters' ] )
|| empty( $wp_query->query['s'] )
) {
return;
@jchristopher
jchristopher / searchwp-customizations.php
Created Mar 3, 2021
Add secondary sort to SearchWP results to sort matching relevance results by Title in ASC order
View searchwp-customizations.php
<?php
// Add secondary sort to SearchWP results to sort matching
// relevance results by Title in ASC order.
add_filter( 'searchwp\query\mods', function( $mods, $query ) {
global $wpdb;
$mod = new \SearchWP\Mod();
$mod->set_local_table( $wpdb->posts );
@jchristopher
jchristopher / searchwp-customizations.php
Created Mar 2, 2021
Limit SearchWP Native/Default results to Category that has 'foobar' slug
View searchwp-customizations.php
<?php
// Limit SearchWP Native/Default results to Category that has 'foobar' slug.
add_filter( 'searchwp\native\args', function( $args, $query ) {
if ( ! isset( $args['tax_query'] ) || ! is_array( $args['tax_query'] ) ) {
$args['tax_query'] = [];
}
$args['tax_query'][] = [
'taxonomy' => 'category',
@jchristopher
jchristopher / searchwp-customizations.php
Created Mar 2, 2021
Limit SearchWP results to Category that has 'foobar' slug
View searchwp-customizations.php
<?php
// Limit SearchWP results to Category that has 'foobar' slug.
$search = new \SWP_Query( [
's' => 'coffee', // Search string.
'tax_query' => [ [
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'foobar',
] ],
@jchristopher
jchristopher / searchwp-customizations.php
Created Mar 1, 2021
Tell SearchWP to index Gmedia (Grand Media) Tags alongside Gmedia Albums
View searchwp-customizations.php
<?php
// Add a SearchWP custom Custom Field to append Gmedia Media Tags to Gmedia Albums when
// indexing, making Gmedia Tags searchable, with Albums coming up for search results.
// @link https://wordpress.org/plugins/grand-media/
add_filter( 'searchwp\entry\data', function( $data, \SearchWP\Entry $entry ) {
global $gmDB, $wpdb;
if ( 'post' . SEARCHWP_SEPARATOR . 'gmedia_album' !== $entry->get_source()->get_name() ) {
return $data;