Last active
January 7, 2022 15:09
-
-
Save rfmeier/6091158 to your computer and use it in GitHub Desktop.
Enable taxonomy wildcard name searching within WordPress search.
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 | |
/** | |
* 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 ); |
Thank you, you are the best
Great thanks 👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.