Skip to content

Instantly share code, notes, and snippets.

@jchristopher
jchristopher / searchwp-customizations.php
Created August 25, 2020 17:12
Use a supplemental SearchWP Engine for BetterDocs searches
<?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 August 25, 2020 12:00
Starter template for SearchWP Term Archive Priority results
<?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;
<?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 July 30, 2020 17:15
Log backtrace for SearchWP Post edit event
<?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 );
<?php
add_filter( 'searchwp\source\gravity_forms\entry\raw', '__return_true' );
@jchristopher
jchristopher / page.php
Last active July 27, 2020 14:50
Outputting SearchWP results that are Gravity Forms Entries
<?php
$searchwp = new \SearchWP\Query( 'marketing', [
'engine' => 'gravity',
'fields' => 'all',
] );
foreach ( $searchwp->results as $result ) {
switch ( get_class( $result ) ) {
case 'SearchWP\Sources\GravityForms\Entry':
@jchristopher
jchristopher / search-results.php
Last active February 25, 2022 14:46
Output SearchWP Live Ajax Search results grouped by Category taxonomy term
<?php if ( ! have_posts() ) : ?>
<p>No results</p>
<?php endif; return; ?>
<?php
$grouped_results = [];
$no_term_results = [];
while ( have_posts() ) {
@jchristopher
jchristopher / search-results.php
Created July 19, 2020 13:16
Show categories with SearchWP Live Ajax Search Results
<?php
/**
* Search results are contained within a div.searchwp-live-search-results
* which you can style accordingly as you would any other element on your site
*
* Some base styles are output in wp_footer that do nothing but position the
* results container and apply a default transition, you can disable that by
* adding the following to your theme's functions.php:
*
* add_filter( 'searchwp_live_search_base_styles', '__return_false' );
@jchristopher
jchristopher / searchwp-customizations.php
Last active April 8, 2021 13:13
How to Integrate SearchWP with Content Imports
<?php
// Disable SearchWP's automatic integration with WP All Import.
add_filter( 'searchwp\integration\wp-all-import', '__return_false' );
add_action( 'pmxi_before_xml_import', function( $import_id ) {
\SearchWP::$indexer->pause();
}, 10 );
add_action( 'pmxi_saved_post', function( $post_id ) {
<?php
/**
* This is an (arguably messy) setup to test what might be a hook issue.
*
* Jchristopher_Hook_ID_Test_Alpha stores an array of strings to prepend to the_content.
* It has a hook jchristopher_test allowing for other code to modify the array of strings.
* It has its own string 'alpha'.
*
* Jchristopher_Hook_ID_Test_Beta has its own string 'beta' and uses the hook from the