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_engine_customizations( $settings, $query ) { | |
// print_r( $settings ); // uncomment this line to see the settings structure | |
// print_r( $query ); // uncomment this line to see the query structure | |
// Check for Title-only search | |
if ( empty( $_REQUEST['searchwp_limit_to_title'] ) ) { | |
return $settings; | |
} | |
// Set all weights to zero except Title | |
foreach ( $settings as $post_type => $post_type_settings ) { | |
// If the post type isn't enabled, skip to the next | |
if ( empty( $post_type_settings['enabled'] ) ) { | |
continue; | |
} | |
// Set all weights to zero excerpt Titles | |
foreach ( $post_type_settings['weights'] as $content_type => $weight ) { | |
switch ( $content_type ) { | |
case 'title': | |
continue; | |
break; | |
case 'tax': | |
// Taxonomies have a different structure | |
foreach ( $weight as $taxonomy => $taxonomy_weight ) { | |
$settings[ $post_type ]['weights'][ $content_type ][ $taxonomy ] = 0; | |
} | |
break; | |
case 'cf': | |
// Custom Fields have a different structure | |
foreach ( $weight as $custom_field_uid => $custom_field_weight_pair ) { | |
$settings[ $post_type ]['weights'][ $content_type ][ $custom_field_uid ]['weight'] = 0; | |
} | |
break; | |
default: | |
$settings[ $post_type ]['weights'][ $content_type ] = 0; | |
} | |
} | |
} | |
// Self unhook | |
remove_filter( 'searchwp_engine_settings_default', 'my_searchwp_engine_customizations', 10, 2 ); | |
return $settings; | |
} | |
add_filter( 'searchwp_engine_settings_default', 'my_searchwp_engine_customizations', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment