Gewicht zu Einträgen (Beiträgen) innerhalb einer bestimmten Kategorie (Taxonomiebegriff) hinzufügen
Standardmäßig berücksichtigt SearchWP den tatsächlichen Inhalt von Taxonomie-Begriffen bei der Suche. Je nachdem, wie Sie Ihre Website eingerichtet haben, möchten Sie möglicherweise Einträgen unabhängig vom Taxonomie-Begriff zusätzliches Gewicht verleihen, stattdessen aber das Gewicht zu allen Einträgen hinzufügen, denen ein bestimmter Taxonomie-Begriff zugewiesen wurde.
All hooks should be added to your custom SearchWP Customizations Plugin.
| <?php | |
| // Add Weight to Entries (posts) within a Specific Category (taxonomy term) in SearchWP. | |
| // @link https://searchwp.com/documentation/knowledge-base/add-weight-category-tag-term/ | |
| add_filter( 'searchwp\query\mods', function( $mods ) { | |
| global $wpdb; | |
| // Taxonomy bonus weight Mods. | |
| $bonuses = [ [ | |
| 'term_id' => 37, // Term ID to receive extra weight. | |
| 'weight' => 100, // How much extra weight for this term. | |
| ], [ | |
| 'term_id' => 82, // Term ID to receive extra weight. | |
| 'weight' => 200, // How much extra weight for this term. | |
| ] ]; | |
| $term_mods = []; | |
| foreach ( $bonuses as $bonus ) { | |
| $mod = new \SearchWP\Mod(); | |
| $index_alias = $mod->get_foreign_alias(); | |
| $mod->relevance( "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 = {$index_alias}.id | |
| AND {$wpdb->prefix}term_relationships.term_taxonomy_id = {$bonus['term_id']} | |
| LIMIT 1 | |
| ) > 0, {$bonus['weight']}, 0)" ); | |
| $term_mods[] = $mod; | |
| } | |
| $mods = array_merge( $mods, $term_mods ); | |
| return $mods; | |
| } ); |

