Skip to content

Instantly share code, notes, and snippets.

View swp1352784.php
<?php
// Step 1: tell SearchWP to index Drafts, Pending, and Private entries in addition to its default post stati.
add_filter( 'searchwp\post_stati', function( $post_stati, $args ) {
$post_stati[] = 'draft';
$post_stati[] = 'pending';
$post_stati[] = 'private';
return $post_stati;
}, 20, 2 );
@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 / 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;
}
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 );