Skip to content

Instantly share code, notes, and snippets.

Jon Christopher jchristopher

Block or report user

Report or block jchristopher

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@jchristopher
jchristopher / functions.php
Last active Jul 29, 2019
Change the WordPress search query parameter from 's' to something else
View functions.php
<?php
add_filter('init', function(){
global $wp;
$wp->add_query_var( 'search_query' );
$wp->remove_query_var( 's' );
} );
@jchristopher
jchristopher / functions.php
Created Jul 25, 2019
Give bonus weight to WooCommerce total sales in SearchWP
View functions.php
<?php
add_filter( 'searchwp_weight_mods', function( $sql ) {
// Multiply total number of sales by 10 to add bonus weight.
$multiplier = 10;
return $sql .= " + IF(swpwcsalesboost.meta_value+0 > 0, (swpwcsalesboost.meta_value+0) * {$multiplier}, 0)";
} );
add_filter( 'searchwp_query_join', function( $sql, $engine ) {
@jchristopher
jchristopher / functions.php
Created Jul 25, 2019
Conditionally force OR logic in SearchWP based on search query
View functions.php
<?php
// If someone searches for "soccer ball" force OR logic in SearchWP.
add_filter( 'searchwp_terms', function( $query, $engine ) {
if ( false !== strpos( strtolower( $query ), 'soccer ball' ) ) {
add_filter( 'searchwp_and_logic', '__return_false' );
}
return $query;
}, 10, 2 );
@jchristopher
jchristopher / functions.php
Created Jul 24, 2019
Give bonus weight to any post that has an ACF Checkbox field ticked
View functions.php
<?php
add_filter( 'searchwp_query_join', function( $sql, $engine ) {
global $wpdb;
$my_meta_key = 'proprietary';
return $sql . " LEFT JOIN {$wpdb->postmeta} AS swp9633meta ON {$wpdb->posts}.ID = swp9633meta.post_id AND swp9633meta.meta_key = '{$my_meta_key}'";
}, 10, 2 );
@jchristopher
jchristopher / functions.php
Last active Jul 10, 2019
Tell FacetWP to skip SearchWP when an empty s query parameter is passed
View functions.php
<?php
// Tell FacetWP to disregard SearchWP if the search query is empty.
add_filter( 'facetwp_facets', function( $facets ) {
if ( ! isset( $_REQUEST['s'] ) || ! empty( $_REQUEST['s'] ) || empty( $facets ) || ! is_array( $facets ) ) {
return $facets;
}
foreach ( $facets as $key => $facet ) {
if ( 'search' !== $facet['type'] ) {
@jchristopher
jchristopher / functions.php
Created Jul 9, 2019
Prevent WP-Views from overriding SearchWP's results
View functions.php
<?php
// Prevent WP-Views from overriding SearchWP's results.
add_filter( 'wpv_filter_wpv_override_wordpress_archive', function( $wpa_to_apply, $wpa_slug ) {
if ( 'view_search-page' !== $wpa_slug ) {
return $wpa_to_apply;
}
return false;
}, 999, 2 );
@jchristopher
jchristopher / functions.php
Created Jul 8, 2019
Integrate SearchWP with SEO Framework local search exclusion
View functions.php
<?php
function my_searchwp_seo_framework_integration( $ids ) {
$post_ids = get_posts( array(
'post_type' => 'any',
'fields' => 'ids',
'nopaing' => true,
'meta_query' => array(
array(
'key' => 'exclude_local_search',
@jchristopher
jchristopher / functions.php
Created Jun 26, 2019
Customize the directory used in SearchWP Modal Form's template loader
View functions.php
<?php
add_filter( 'searchwp_modal_form_template_dir', function( $dir ) {
return 'my-searchwp-modal-forms';
} );
View tmp.sh
# Build development version of SearchWP Modal Search Form bundle
npm run dev
# Build production version of SearchWP Modal Search Form bundle
npm run build
# Build development version of SearchWP Modal Search Form block
npm run blockdev
# Build production version of SearchWP Modal Search Form block
View functions.php
<?php
searchwp_modal_form_trigger( array(
'echo' => true, // Whether to echo
'type' => 'button', // Either 'link' or 'button'
'text' => 'Open Search', // Text of the trigger
'engine' => 'my_searchwp_engine', // SearchWP engine (when applicable)
'template' => 'My Custom Template', // Modal template to use (see docs)
) );
You can’t perform that action at this time.