-
-
Save searchwpgists/158c1fe2da44638c7752530ff31c1df3 to your computer and use it in GitHub Desktop.
Add Relevance Weight to ACF True/False Checkbox Fields in SearchWP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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