Last active
March 14, 2019 13:54
-
-
Save jchristopher/66a24f8c2faeb0a3a76739919fb90b71 to your computer and use it in GitHub Desktop.
Give extra weight to entries within a Category, Tag, or Taxonomy Term 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 | |
function my_searchwp_weight_mods( $sql ) { | |
global $wpdb; | |
$swp_db_prefix = $wpdb->prefix . SEARCHWP_DBPREFIX; | |
// These are the taxonomy terms we want to give bonus weight. | |
$bonuses = array( | |
array( | |
'term_id' => 17, // The taxonomy term ID to give the bonus weight. | |
'bonus_weight' => 1000, // The bonus weight to give this taxonomy term. | |
), | |
array( | |
'term_id' => 88, // The taxonomy term ID to give the bonus weight. | |
'bonus_weight' => 300, // The bonus weight to give this taxonomy term. | |
), | |
); | |
foreach ( $bonuses as $bonus ) { | |
$sql .= " + ( IF (( | |
SELECT {$wpdb->prefix}posts.ID | |
FROM {$wpdb->prefix}posts | |
LEFT JOIN {$wpdb->prefix}term_relationships ON ( | |
{$wpdb->prefix}posts.ID = {$wpdb->prefix}term_relationships.object_id | |
) | |
WHERE {$wpdb->prefix}posts.ID = {$swp_db_prefix}index.post_id | |
AND {$wpdb->prefix}term_relationships.term_taxonomy_id = {$bonus['term_id']} | |
LIMIT 1 | |
) > 0, {$bonus['bonus_weight']}, 0 )) "; | |
} | |
return $sql; | |
} | |
add_filter( 'searchwp_weight_mods', 'my_searchwp_weight_mods', 20 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment