Skip to content

Instantly share code, notes, and snippets.

Avatar

Jon Christopher jchristopher

View GitHub Profile
@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();
@jchristopher
jchristopher / searchwp-customizations.php
Last active Sep 18, 2020
Tell SearchWP to index Posts from ACF Relationship Field
View searchwp-customizations.php
<?php
// Customize ACF Relationship field data. By default an array of IDs is stored,
// this customization will instead tell SearchWP to index the full WP_Post object.
add_filter( 'searchwp\source\post\attributes\meta', function( $meta_value, $data ) {
$acf_field_names = [
'acf_relationship_field_name_1',
'acf_relationship_field_name_2',
'acf_relationship_field_name_3',
];
@jchristopher
jchristopher / searchwp-customizations.php
Created Sep 10, 2020
Tell SearchWP to parse WooCommerce Downloadable Product downloads for document content
View searchwp-customizations.php
<?php
// Tell SearchWP to parse WooCommerce Downloadable Product downloads for document content.
// The content will be extracted from downloadable documents where possible and stored
// in SearchWP's index as a Custom Field with a label of "SearchWP WooCommerce Download Content"
add_filter( 'searchwp\entry\data', function( $data, \SearchWP\Entry $entry ) {
if ( 'post.product' !== $entry->get_source()->get_name() || ! class_exists( 'WC_Product' ) ) {
return $data;
}
@jchristopher
jchristopher / searchwp-customizations.php
Created Aug 31, 2020
Force quoted search logic in SearchWP when applicable
View searchwp-customizations.php
<?php
// Force multiple word searches to use quoted search logic if quotes are not added.
// NOTE: Quoted search must be enabled (checkbox on the Advanced tab)
add_filter(
'searchwp\query\search_string',
function( $search_string, $query ) {
// If there are already quotes, bail out.
if ( false !== strpos( $search_string, '"' ) ) {
return $search_string;
@jchristopher
jchristopher / searchwp-customizations.php
Created Aug 25, 2020
Use a supplemental SearchWP Engine for BetterDocs searches
View searchwp-customizations.php
<?php
// Use a supplemental SearchWP Engine for BetterDocs searches.
add_filter( 'searchwp_betterdocs_engine', function( $engine, $params ) {
return 'my_engine_name';
}, 20, 2 );
@jchristopher
jchristopher / template-searchwp-term-archive-priority-sample.php
Created Aug 25, 2020
Starter template for SearchWP Term Archive Priority results
View template-searchwp-term-archive-priority-sample.php
<?php
/* Template Name: SearchWP Term Archive Priority Sample */
global $post;
// Retrieve applicable query parameters.
$search_query = isset( $_GET['searchwp'] ) ? sanitize_text_field( $_GET['searchwp'] ) : null;
$search_page = isset( $_GET['swppg'] ) ? absint( $_GET['swppg'] ) : 1;
View searchwp-customizations.php
<?php
// Tell SearchWP to drop Media with no parent when Parent Attribution has been enabled.
// This was the default behavior in SearchWP 3 but has changed in SearchWP 4.
add_filter( 'searchwp\source\post\attachment\parent_attribution\strict', '__return_true' );
@jchristopher
jchristopher / searchwp-customizations.php
Created Jul 30, 2020
Log backtrace for SearchWP Post edit event
View searchwp-customizations.php
<?php
add_action( 'searchwp\source\post\drop', function( $args ) {
do_action( 'searchwp\debug\log', "Drop event for post {$args['post_id']}", 'backtrace' );
$e = new \Exception();
$trace = explode( "\n", $e->getTraceAsString() );
$trace = array_reverse( $trace );
array_shift( $trace );
array_pop( $trace );
View searchwp-customizations.php
<?php
add_filter( 'searchwp\source\gravity_forms\entry\raw', '__return_true' );
@jchristopher
jchristopher / page.php
Last active Jul 27, 2020
Outputting SearchWP results that are Gravity Forms Entries
View page.php
<?php
$searchwp = new \SearchWP\Query( 'marketing', [
'engine' => 'gravity',
'fields' => 'all',
] );
foreach ( $searchwp->results as $result ) {
switch ( get_class( $result ) ) {
case 'SearchWP\Sources\GravityForms\Entry':
You can’t perform that action at this time.