Skip to content

Instantly share code, notes, and snippets.

Avatar

Jon Christopher jchristopher

View GitHub Profile
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':
@jchristopher
jchristopher / search-results.php
Last active Jul 19, 2020
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 / search-results.php
Created Jul 19, 2020
Show categories with SearchWP Live Ajax Search Results
View search-results.php
<?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 Jul 13, 2020
How to Integrate SearchWP with Content Imports
View searchwp-customizations.php
<?php
/**
* WP All Import SearchWP PRE-import routine
*
* @link https://searchwp.com/documentation/knowledge-base/content-imports/
*/
add_action( 'pmxi_before_xml_import', function( $import_id ) {
\SearchWP::$indexer->pause();
View functions.php
<?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
@jchristopher
jchristopher / searchwp-customizations.php
Created Jul 7, 2020
Add support for WooCommerce Admin filters when searching Orders with SearchWP
View searchwp-customizations.php
<?php
// Add support for WooCommerce Admin filters when searching Orders with SearchWP.
add_filter( 'searchwp\query\mods', function( $mods, $query ) {
global $wpdb;
if ( isset( $_GET['_customer_user'] ) && ! empty( $_GET['_customer_user'] ) ) {
$mod = new \SearchWP\Mod( \SearchWP\Utils::get_post_type_source_name( 'shop_order' ) );
$mod->set_local_table( $wpdb->postmeta );
$mod->on( 'post_id', [ 'column' => 'id' ] );
@jchristopher
jchristopher / searchwp-customizations.php
Last active Jul 6, 2020
Use Apache Tika to extract PDF content in SearchWP
View searchwp-customizations.php
<?php
// Use Apache Tika to extract PDF content in SearchWP.
add_filter( 'searchwp\parser\pdf', function( $content, $args ) {
// Ensure this path is updated to match your Tika installation path!
$path_to_tika = '/srv/bin/tika-app-1.18.jar';
// Execute the command.
$cmd = "java -jar {$path_to_tika} -t {$args['file']}";
You can’t perform that action at this time.