Skip to content

Instantly share code, notes, and snippets.

@jchristopher
jchristopher / searchwp-customizations.php
Created July 7, 2020 11:46
Add support for WooCommerce Admin filters when searching Orders with SearchWP
<?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 July 6, 2020 12:41
Use Apache Tika to extract PDF content in SearchWP
<?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 June 30, 2020 00:18
Limit SearchWP Product searches to the first 30 characters of the Title
<?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
Last active June 9, 2021 01:00
Add WooCommerce Order item data to WooCommerce Orders Search in SearchWP
<?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 June 24, 2020 19:29
Remove parsed PDF content when rebuilding SearchWP index
<?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 June 15, 2020 16:30
Add private Custom Post Types to SearchWP.
<?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;
@jchristopher
jchristopher / searchwp-customizations.php
Last active June 4, 2021 00:52
Sort SearchWP Post, Page, and Custom Post Type Results by date in DESC order
<?php
// Sort SearchWP Post, Page, and Custom Post Type Results by date in DESC order.
add_filter( 'searchwp\query\mods', function( $mods, $query ) {
foreach ( $query->get_engine()->get_sources() as $source ) {
$flag = 'post' . SEARCHWP_SEPARATOR;
if ( 'post.' !== substr( $source->get_name(), 0, strlen( $flag ) ) ) {
continue;
}
@jchristopher
jchristopher / searchwp-customizations.php
Last active July 2, 2020 16:50
Append attached PDF content to 'parent' post during indexing in SearchWP
<?php
// @link https://searchwp.com/documentation/knowledge-base/append-document-content-to-parent-post-content/
// Retrieve child PDF content and add as 'extra' data to a SearchWP Entry.
add_filter( 'searchwp\entry\data', function( $data, \SearchWP\Entry $entry ) {
// Convert the SearchWP Entry into it's native object type.
$entry = $entry->native();
// We only want to consider WP_Post objects.
if ( ! $entry instanceof \WP_Post ) {
@jchristopher
jchristopher / searchwp-customizations.php
Created June 3, 2020 13:34
Index Mod vs. Source Mod in SearchWP
<?php
// @link https://searchwp.com/documentation/knowledge-base/comparing-index-source-mods/
// Instantiate an index Mod that applies to the entire index.
$mod = new \SearchWP\Mod();
// Instantiate a Source Mod that applies only to Posts.
$source = \SearchWP\Utils::get_post_type_source_name( 'post' );
$mod = new \SearchWP\Mod( $source );
@jchristopher
jchristopher / searchwp-customizations.php
Created June 2, 2020 13:48
Drop Media from results that have no parent when parent attribution has been enabled in SearchWP.
<?php
// Drop Media from results that have no parent when parent attribution has been enabled in SearchWP.
// @link https://searchwp.com/documentation/hooks/searchwp-source-post-post_type-parent_attribution-strict
add_filter( 'searchwp\source\post\attachment\parent_attribution\strict', '__return_true' );