Advertisement
keha76

WordPress Search (Incl. tax & terms)

Feb 24th, 2013
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.02 KB | None | 0 0
  1. <?php
  2. /*
  3.  * Include custom taxonomies and terms in the default WordPress search
  4.  */
  5.  
  6.  
  7. function keha76_search_where($where){
  8.   global $wpdb;
  9.   if (is_search())
  10.     $where .= "OR (t.name LIKE '%".get_search_query()."%' AND {$wpdb->posts}.post_status = 'publish')";
  11.   return $where;
  12. }
  13.  
  14. function keha76_search_join($join){
  15.   global $wpdb;
  16.   if (is_search())
  17.     $join .= "LEFT JOIN {$wpdb->term_relationships} tr ON {$wpdb->posts}.ID = tr.object_id INNER JOIN {$wpdb->term_taxonomy} tt ON tt.term_taxonomy_id=tr.term_taxonomy_id INNER JOIN {$wpdb->terms} t ON t.term_id = tt.term_id";
  18.   return $join;
  19. }
  20.  
  21. function keha76_search_groupby($groupby){
  22.   global $wpdb;
  23.   $groupby_id = "{$wpdb->posts}.ID";
  24.   if(!is_search() || strpos($groupby, $groupby_id) !== false) return $groupby;
  25.   if(!strlen(trim($groupby))) return $groupby_id;
  26.   return $groupby.", ".$groupby_id;
  27. }
  28.  
  29. add_filter('posts_where','keha76_search_where');
  30. add_filter('posts_join', 'keha76_search_join');
  31. add_filter('posts_groupby', 'keha76_search_groupby');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement