Skip to content

Instantly share code, notes, and snippets.

@jchristopher
Last active March 14, 2019 13:54
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/66a24f8c2faeb0a3a76739919fb90b71 to your computer and use it in GitHub Desktop.
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
<?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