Skip to content

Instantly share code, notes, and snippets.

@rfmeier
Last active January 7, 2022 15:09
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rfmeier/6091158 to your computer and use it in GitHub Desktop.
Save rfmeier/6091158 to your computer and use it in GitHub Desktop.
Enable taxonomy wildcard name searching within WordPress search.
<?php
/**
* Callback for WordPress 'pre_get_posts' action.
*
* If doing a search, set the post type to 'post'.
*
* @author Ryan Meier http://www.rfmeier.net/
*
* @param WP_Query $query The current WP_Query object.
* @return none
*/
function custom_pre_get_posts( $query ){
if( $query->is_search() && $query->is_main_query() ){
$query->set( 'post_type', 'post' );
}
}
add_action( 'pre_get_posts', 'custom_pre_get_posts' );
/**
* Callback for WordPress 'posts_search' filter.
*
* Prevent post title and post content from being searched
* if the current post type is 'post'.
*
* @author Ryan Meier http://www.rfmeier.net/
*
* @param string $search The posts search string
* @param WP_Query $query The current WP_Query object
* @return string $search The posts search string
*/
function custom_posts_search( $search, $query ){
if( 'post' == $query->get( 'post_type' ) && is_search() && is_main_query() ){
return '';
}
return $search;
}
add_filter( 'posts_search', 'custom_posts_search', 10, 2 );
/**
* Callback for WordPress 'posts_join' filter.
*
* Join term tables to a WordPress search if the specified post
* type is 'post'.
*
* @author Ryan Meier http://www.rfmeier.net/
*
* @param string $join The current where clause JOIN string.
* @param WP_Query $query The current WP_Query object.
* @return string $join The current where clause JOIN string.
*/
function posts_join_taxonomies( $join, $query ){
global $wpdb;
if( 'post' == $query->get( 'post_type' ) && is_search() && is_main_query() ){
$join .= "
INNER JOIN
{$wpdb->term_relationships} ON {$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id
INNER JOIN
{$wpdb->term_taxonomy} ON {$wpdb->term_taxonomy}.term_taxonomy_id = {$wpdb->term_relationships}.term_taxonomy_id
INNER JOIN
{$wpdb->terms} ON {$wpdb->terms}.term_id = {$wpdb->term_taxonomy}.term_id ";
}
return $join;
}
add_filter( 'posts_join', 'posts_join_taxonomies', 10, 2 );
/**
* Callback for WordPress 'posts_where' filter.
*
* Specify wildcard search on taxonomy names if taxonomy is
* within 'post_tag' or 'category'.
*
* @author Ryan Meier http://www.rfmeier.net/
*
* @param String $where The where in the clause
* @param WP_Query $query The current WP_Query object
* @return String $where The where in the clause
*/
function tax_search_where( $where, $query ){
global $wpdb;
if( 'post' == $query->get( 'post_type' ) && is_search() && is_main_query() ){
$where .= " AND (
{$wpdb->term_taxonomy}.taxonomy IN('category', 'post_tag')
AND
{$wpdb->terms}.name LIKE ('%".$wpdb->escape( get_query_var('s') )."%') ) ";
}
return $where;
}
add_filter( 'posts_where', 'tax_search_where', 10, 2 );
/**
* Callback for WordPress 'posts_groupby' filter.
*
* Specify the groupby of the sql clause if doing a search
* and post type is 'post'.
*
* @author Ryan Meier http://www.rfmeier.net/
*
* @param String $groupby The groupby within the sql clause.
* @param WP_Query $query The current WP_Query object.
* @return String $groupby The groupby within the sql clause.
*/
function tax_search_groupby( $groupby, $query ){
global $wpdb;
if( 'post' == $query->get( 'post_type' ) && is_search() && is_main_query() ){
$groupby = "{$wpdb->posts}.ID";
}
return $groupby;
}
add_filter( 'posts_groupby', 'tax_search_groupby', 10, 2 );
@msaggiorato
Copy link

Hey, I found this, and it was just what i needed for a project. Although, maybe it's better to call is_search() and is_main_query() from the $query object, instead of the global function.

Anyhow, thanks.

@alvaroveliz
Copy link

Thank you, you are the best

@jimmylad
Copy link

Great thanks 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment