I have two custom taxonomies applied to two custom post types. the terms list on the sidebar just fine and will list all posts associated with it. However, if you search one of the terms in specific, it doesn't bring up a post with that term.

Example: http://dev.andrewnorcross.com/das/all-case-studies/ Search for term "PQRI"

I get nothing. Any ideas? I've tried using various search plugins but they either break my custom search parameters or just don't work.

link|improve this question

77% accept rate
Nocross, can you add some feedback to the answer proposed by Jan? Are you probably looking for a plugin that does the job? – hakre Nov 6 '10 at 17:13
I ended up ditching the plan. Since I had created 3 separate search functions (based on different needs in certain areas), all the plugins I tested broke those. In the end, I told the client to include terms in the content if they wanted it searchable. – Norcross Nov 13 '10 at 20:25
feedback

3 Answers

up vote 3 down vote accepted

I would recommend the Search Everything plugin too, but if you want to implement this using WP's search function, here's the code I'm using in my Atom theme:

// search all taxonomies, based on: http://projects.jesseheap.com/all-projects/wordpress-plugin-tag-search-in-wordpress-23

function atom_search_where($where){
  global $wpdb;
  if (is_search())
    $where .= "OR (t.name LIKE '%".get_search_query()."%' AND {$wpdb->posts}.post_status = 'publish')";
  return $where;
}

function atom_search_join($join){
  global $wpdb;
  if (is_search())
    $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";
  return $join;
}

function atom_search_groupby($groupby){
  global $wpdb;

  // we need to group on post ID
  $groupby_id = "{$wpdb->posts}.ID";
  if(!is_search() || strpos($groupby, $groupby_id) !== false) return $groupby;

  // groupby was empty, use ours
  if(!strlen(trim($groupby))) return $groupby_id;

  // wasn't empty, append ours
  return $groupby.", ".$groupby_id;
}

add_filter('posts_where','atom_search_where');
add_filter('posts_join', 'atom_search_join');
add_filter('posts_groupby', 'atom_search_groupby');

It's based on the Tag-Search plugin: http://projects.jesseheap.com/all-projects/wordpress-plugin-tag-search-in-wordpress-23

link|improve this answer
thank you, @One Trick Pony. that code pasted into my functions file did the trick! :) – user3947 Mar 15 '11 at 5:11
This is great-- how can this code be modified to exclude an array of taxonomy IDs from the search? – j-man86 Jan 6 at 20:23
feedback

Is this the standard WordPress search? Because that doesn't seem to include taxonomies (not even standard, like categories and tags) in the search. The code searches in post_title and post_content, but if you want to include anything else you should hook into the posts_search filter.

link|improve this answer
feedback

I have the same level of information like Jan. I know it's possible to extend search with plugins as well.

Probably Search Everything (Wordpress Plugin) is what you are looking for. According to the feature list, it now supports custom taxonomies.

link|improve this answer
+1 For Search Everything plugin. It works as expected and returns more results than standard Wordpress search. – PNMG Dec 2 '10 at 22:00
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.