Created
May 12, 2016 01:26
-
-
Save joelstransky/5cd0a3379973c00044f3dbef2c15238f to your computer and use it in GitHub Desktop.
searching with taxonomy terms in Wordpress
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
function document_posts_search_filter( $query ) { | |
if ( !is_admin() && $query->is_main_query() ) { | |
if ($query->is_search) { | |
$tax_args = array( 'relation' => 'OR' ); | |
// document types taxonomy | |
$doctypes = explode(",", get_query_var( 'doctype' ) ); | |
if ( isset( $_GET['doctype'] ) && $_GET['doctype'] != "" ) { | |
$tax_args[] = array( | |
'taxonomy' => 'document_type', | |
'field' => 'slug', | |
'terms' => $doctypes, | |
'operator' => 'IN', | |
); | |
} | |
$query->set( 'tax_query', $tax_args ); | |
} | |
$query->set( 'post_type', array( 'document_cpt' ) ); | |
} | |
return $query; | |
} | |
add_filter( 'pre_get_posts', 'document_posts_search_filter' ); |
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
// http://stackoverflow.com/questions/13491828/how-to-amend-wordpress-search-so-it-queries-taxonomy-terms-and-category-terms | |
function tax_search_join( $join, $wp_query) | |
{ | |
global $wpdb; | |
if( is_search() ) | |
{ | |
$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; | |
} | |
function tax_search_where( $where ) | |
{ | |
global $wpdb; | |
if( is_search() ) | |
{ | |
// add the search term to the query | |
$where .= " OR | |
( | |
{$wpdb->term_taxonomy}.taxonomy LIKE 'document_tags' | |
AND | |
" . $wpdb->prepare( "{$wpdb->terms}.name LIKE '%%%s%%'", get_query_var('s') ) . " | |
) "; | |
} | |
return $where; | |
} | |
function tax_search_groupby( $groupby ) | |
{ | |
global $wpdb; | |
if( !is_search() ) { | |
return $groupby; | |
} | |
// we need to group on post ID | |
$search_group_by = "{$wpdb->posts}.ID"; | |
if( preg_match( "/$search_group_by/", $groupby )) { | |
// grouping we need is already there | |
return $groupby; | |
} | |
if( !strlen(trim($groupby))) { | |
// groupby was empty, use ours | |
return $search_group_by; | |
} | |
// wasn't empty, append ours | |
return $groupby . ", " . $search_group_by; | |
} | |
add_filter('posts_join', 'tax_search_join', 10, 2); | |
add_filter('posts_where', 'tax_search_where'); | |
add_filter('posts_groupby', 'tax_search_groupby'); |
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
// Not unique table/alias: 'wp_term_relationships' | |
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID | |
FROM wp_posts | |
INNER JOIN wp_term_relationships | |
ON (wp_posts.ID = wp_term_relationships.object_id) | |
INNER JOIN wp_term_relationships | |
ON wp_posts.ID = wp_term_relationships.object_id | |
INNER JOIN wp_term_taxonomy | |
ON wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id | |
INNER JOIN wp_terms | |
ON wp_terms.term_id = wp_term_taxonomy.term_id | |
WHERE 1=1 | |
AND ( wp_term_relationships.term_taxonomy_id IN (3) ) | |
AND (((wp_posts.post_title LIKE '%melon%') | |
OR (wp_posts.post_excerpt LIKE '%melon%') | |
OR (wp_posts.post_content LIKE '%melon%'))) | |
AND wp_posts.post_type = 'document_cpt' | |
AND (wp_posts.post_status = 'publish' | |
OR wp_posts.post_status = 'acf-disabled' | |
OR wp_posts.post_status = 'private') | |
OR ( wp_term_taxonomy.taxonomy LIKE 'document_tags' | |
AND wp_terms.name LIKE '%melon%' ) | |
GROUP BY wp_posts.ID | |
ORDER BY wp_posts.post_title LIKE '%melon%' DESC, wp_posts.post_date DESC | |
LIMIT 0, 20 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment