Skip to content

Instantly share code, notes, and snippets.

@jchristopher
jchristopher / searchwp-customizations.php
Last active October 3, 2020 01:54
Ranomize SearchWP 4 results (with pagination)
<?php
// In order to paginate randomized results, we need a seed.
add_action( 'init', function() {
if ( ! isset( $_COOKIE['seed'] ) ) {
setcookie( 'seed', rand(), 0, COOKIEPATH, COOKIE_DOMAIN );
}
} );
// Tell SearchWP to randomize the results based on the stored seed.
@jchristopher
jchristopher / ig.php
Created October 1, 2020 17:28
Store your recent Instagram posts as a WordPress transient. Simple. Might break. ¯\_(ツ)_/¯
<?php
// Super simple transient storage of the 4 most recent Instagramp posts.
// Stores only images/first image in carousel album.
// Assumes you already have an access token.
// Don't know if the access token invalidates over time. If it does, this doesn't handle it.
// Transient storage is an array of media objects directly from Instagram.
// Possible to sideload images into the Media library using https://developer.wordpress.org/reference/functions/media_sideload_image/
$transient_name = 'my_instagram_feed';
@jchristopher
jchristopher / searchwp-customizations.php
Last active September 30, 2020 16:44
Include WooCommerce Product Drafts in SearchWP Admin Searches
<?php
// Add Drafts to Products when indexing and searching in Admin.
add_filter( 'searchwp\post_stati\product', function( $stati ) {
$stati[] = 'draft';
$stati[] = 'private';
return $stati;
}, 30 );
@jchristopher
jchristopher / searchwp-customizations.php
Created September 29, 2020 01:08
Exclude email addresses from SearchWP's index
<?php
add_filter( 'searchwp\tokens', function( $tokens ) {
return array_filter( $tokens, function( $token ) {
return ! is_email( $token );
} );
}, 5 );
@jchristopher
jchristopher / searchwp-customizations.php
Created September 29, 2020 00:49
Add support for sorting by Name when searching WooCommerce Products in the Admin Area with SearchWP
<?php
add_filter( 'searchwp\query\mods', function( $mods ) {
if ( ! is_admin() ) {
return $mods;
}
if ( empty( $_GET['orderby'] || empty( $_GET['order'] ) ) {
return $mods;
}
@jchristopher
jchristopher / searchwp-customizations.php
Last active November 19, 2020 15:38
Index WooCommerce Product Variations with Products in SearchWP
<?php
// Add WooCommerce Product Variation data to parent Products in SearchWP.
add_filter( 'searchwp\entry\data', function( $data, \SearchWP\Entry $entry ) {
// If this is not a Product, there's nothing to do.
if ( 'product' !== get_post_type( $entry->get_id() ) ) {
return $data;
}
$my_extra_meta_key = 'searchwp_product_variations';
@jchristopher
jchristopher / searchwp-customizations.php
Last active September 18, 2020 00:36
Tell SearchWP to give more weight to WP_Posts with more comments
<?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 September 18, 2020 12:00
Tell SearchWP to index Posts from ACF Relationship Field
<?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 September 10, 2020 17:05
Tell SearchWP to parse WooCommerce Downloadable Product downloads for document content
<?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 August 31, 2020 12:48
Force quoted search logic in SearchWP when applicable
<?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;