|
<?php |
|
/** |
|
* Plugin Name: GG Custom Results |
|
* Description: Limits search results for specified search terms to certain posts. |
|
* Author: Galen Gidman |
|
* Author URI: https://galengidman.com/ |
|
*/ |
|
|
|
/** |
|
* Hooks into pre_get_posts to alter the `WP_Query` object to return custom search results for specific terms. |
|
* |
|
* @param object $wp_query `WP_Query` object. |
|
* @return object `WP_Query` object, possibly modified to return custom search results |
|
*/ |
|
function ggcr_custom_results( $wp_query ) { |
|
|
|
// Bail early if this isn't a WP_Query object for a front-end search query |
|
if ( is_admin() || ! $wp_query->is_search() ) { |
|
return; |
|
} |
|
|
|
// Also bail if no custom results have been configured, or they have been defined but not as an array |
|
if ( ! defined( 'GG_CUSTOM_RESULTS' ) || ! is_array( GG_CUSTOM_RESULTS ) ) { |
|
return; |
|
} |
|
|
|
// Get custom search results as defined in `wp-config.php` |
|
$searches = GG_CUSTOM_RESULTS; |
|
|
|
// The search term |
|
$search_term = ggcr_clean_term( get_query_var( 's' ) ); |
|
|
|
// Assume there aren't custom results by default |
|
$custom_results = false; |
|
|
|
// Loop through searches |
|
foreach ( $searches as $key => $results ) { |
|
|
|
// If the user search term matches one of the custom results... |
|
if ( $search_term == ggcr_clean_term( $key ) ) { |
|
|
|
// Assign those results to $custom_results |
|
$custom_results = $results; |
|
|
|
// And stop the foreach loop |
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
// If custom results were found for the search term... |
|
if ( $custom_results ) { |
|
|
|
// If there is only one custom result for that query... |
|
if ( count( $custom_results ) === 1 ) { |
|
|
|
// Generate the permalink for it |
|
$permalink = esc_url( get_permalink( $custom_results[0] ) ); |
|
|
|
// And redirect directly to it, skipping the search results page |
|
wp_redirect( $permalink, 302 ); |
|
exit; |
|
|
|
} |
|
|
|
// Otherwise, update the query object to only return our custom results |
|
else { |
|
$wp_query->set( 's', false ); |
|
$wp_query->set( 'post__in', $custom_results ); |
|
$wp_query->set( 'ignore_sticky_posts', true ); |
|
} |
|
|
|
} |
|
|
|
// Return the query object |
|
return $wp_query; |
|
|
|
} |
|
add_action( 'pre_get_posts', 'ggcr_custom_results' ); |
|
|
|
/** |
|
* Conditionally replaces value of `get_search_query()` with URL paramater since we unset it to use custom search results. |
|
* |
|
* Possibly the most hacky code on the planet. |
|
* |
|
* @return string Search query. |
|
*/ |
|
function ggcr_fix_missing_search_query( $query ) { |
|
|
|
// If this is a search query, but the query value is set to false (because we are returning custom results) |
|
if ( is_search() && $query === false ) { |
|
|
|
// Set the query to value of the `s` URL paramater |
|
$query = esc_attr( $_GET['s'] ); |
|
|
|
} |
|
|
|
// Return the query |
|
return $query; |
|
|
|
} |
|
add_filter( 'get_search_query', 'ggcr_fix_missing_search_query' ); |
|
|
|
/** |
|
* Helper to clean search terms. Converts to lowercase and trims whitespace from each end. |
|
* |
|
* @param string $term Search term to clean. |
|
* @return string Cleaned search term. |
|
*/ |
|
function ggcr_clean_term( $term = '' ) { |
|
|
|
return trim( strtolower( $term ) ); |
|
|
|
} |