Skip to content

Instantly share code, notes, and snippets.

Avatar

Jon Christopher jchristopher

View GitHub Profile
@jchristopher
jchristopher / search-results.php
Last active Aug 7, 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']}";
@jchristopher
jchristopher / searchwp-customizations.php
Created Jun 30, 2020
Limit SearchWP Product searches to the first 30 characters of the Title
View searchwp-customizations.php
<?php
// Limit SearchWP Product searches to the first 30 characters of the Title.
add_filter( 'searchwp\entry\data', function( $data, $entry ) {
$source = \SearchWP\Utils::get_post_type_source_name( 'product' );
if ( $source === substr( $entry->get_source()->get_name(), 0, strlen( $source ) ) ) {
$data['title'] = isset( $data['title'] ) ? substr( $data['title'], 0, 30 ) : '';
}
@jchristopher
jchristopher / searchwp-customizations.php
Created Jun 29, 2020
Add WooCommerce Order item data to WooCommerce Orders Search in SearchWP
View searchwp-customizations.php
<?php
// Add WooCommerce Order item data to WooCommerce Orders Search in SearchWP.
// @link https://searchwp.com/documentation/knowledge-base/search-woocommerce-orders/
add_filter( 'searchwp\entry\data', function( $data, \SearchWP\Entry $entry ) {
// The Product data keys to index for each Order item.
$data_to_index = [
'sku',
'name',
'slug',
@jchristopher
jchristopher / searchwp-customizations.php
Created Jun 24, 2020
Remove parsed PDF content when rebuilding SearchWP index
View searchwp-customizations.php
<?php
// Remove parsed PDF content when rebuilding SearchWP index.
add_action( 'searchwp\index\rebuild', function() {
global $wpdb;
$wpdb->delete( $wpdb->prefix . 'postmeta', array( 'meta_key' => SEARCHWP_PREFIX . 'content' ) );
$wpdb->delete( $wpdb->prefix . 'postmeta', array( 'meta_key' => SEARCHWP_PREFIX . 'pdf_metadata' ) );
} );
@jchristopher
jchristopher / searchwp-customizations.php
Created Jun 15, 2020
Add private Custom Post Types to SearchWP.
View searchwp-customizations.php
<?php
// Add private Custom Post Types to SearchWP.
add_filter( 'searchwp\sources', function( $sources ) {
// Create a Source from each private CPT e.g.
$sources[] = new \SearchWP\Sources\Post( 'my_private_cpt_name_1' );
$sources[] = new \SearchWP\Sources\Post( 'my_private_cpt_name_2' );
$sources[] = new \SearchWP\Sources\Post( 'my_private_cpt_name_3' );
return $sources;
You can’t perform that action at this time.