Skip to content

Instantly share code, notes, and snippets.

@jchristopher jchristopher/functions.php
Last active Mar 14, 2019

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