Skip to content

Instantly share code, notes, and snippets.

@galengidman galengidman/README.md
Last active Jun 22, 2017

Embed
What would you like to do?
Quick-and-dirty plugin that allows you to specify custom search results for specific search queries.

GG Custom Results

Quick-and-dirty plugin that allows you to specify custom search results for specific search queries.

Installation

Move gg-custom-results.php into either the /wp-content/plugins/ or /wp-content/mu-plugins/ directory. If you put it in plugins, you'll need to activate from Plugins menu in the WordPress admin. If you put in mu-plugins, it will be automatically active.

Configuration

You can define your custom search results in wp-config.php. Insert the following just above the line that reads /* That's all, stop editing! Happy blogging. */:

define( 'GG_CUSTOM_RESULTS', array(
	'custom search query' => array( 10, 20 ),
	'another query' => array( 11 ),
) );

The key of each item in the array (e.g.: custom search query) is the query you want to display custom results for. The value (e.g.: array( 10, 20 )) is an array post (or page) IDs you want to return as custom results. Note that if you only have one ID for a particular query, the user will be redirected straight to that result upon search and the search results page will be skipped entirely.

Custom Post Types

If the custom results are Custom Post Types, you'll additionally need to ensure that you've configured WordPress to include CPTs in the search results. Here's a good tutorial: https://thomasgriffin.io/how-to-include-custom-post-types-in-wordpress-search-results/

<?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 ) );
}
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.