Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.