Skip to content

Instantly share code, notes, and snippets.

@jchristopher
Created March 30, 2020 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jchristopher/705cb0ce85e2c04a3917fe21760f4aaf to your computer and use it in GitHub Desktop.
Save jchristopher/705cb0ce85e2c04a3917fe21760f4aaf to your computer and use it in GitHub Desktop.
Add Relevance Weight to ACF True/False Checkbox Fields in SearchWP
<?php
// Add Relevance Weight to ACF True/False Checkbox Fields in SearchWP.
// @link https://searchwp.com/documentation/knowledge-base/add-relevance-acf-checkbox/
add_filter( 'searchwp\query\mods', function( $mods ) {
global $wpdb;
$my_meta_key = 'proprietary'; // ACF True/False name.
$bonus_weight = 1000; // Extra weight to add when checkbox is ticked.
$mod = new \SearchWP\Mod();
$mod->set_local_table( $wpdb->postmeta );
$mod->on( 'post_id', [ 'column' => 'id' ] );
$mod->on( 'meta_key', [ 'value' => $my_meta_key ] );
$mod->weight( function( $runtime_mod ) use ( $bonus_weight ) {
$local_alias = $runtime_mod->get_local_table_alias();
return "IF({$local_alias}.meta_value+0 = 1, {$bonus_weight}, 0)";
} );
$mods[] = $mod;
return $mods;
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment