39

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.

2
  • Nocross, can you add some feedback to the answer proposed by Jan? Are you probably looking for a plugin that does the job?
    – hakre
    Commented Nov 6, 2010 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
    Commented Nov 13, 2010 at 20:25

6 Answers 6

46

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

8
  • 1
    This is great-- how can this code be modified to exclude an array of taxonomy IDs from the search? Commented Jan 6, 2012 at 20:23
  • It should be noted that the filter callbacks for these hooks accept 2 arguments; the 2nd for all of them being the WP_Query instance which is passed by reference. Any checks for is_search() or other WP_Query method calls (is_search() is_home() etc.) should always be called directly on the query instance (eg. $query->is_search() assuming the name of the instance variable is $query in the callback signature) rather than the template function which will always refer to the main query, not the query that the filter is running for. Commented Jun 7, 2014 at 2:55
  • 6
    Also, probably not a good idea to inject the raw publicly available search string directly into an SQL query... recommended reading Commented Jun 7, 2014 at 3:03
  • 1
    @EvanMattson — you commented above that it's "probably not a good idea to inject the raw publicly available search string directly into an SQL query." How would go about negating that risk? I read the link you provided but couldn't see how that links to the original answer. Many thanks Em.
    – pjk_ok
    Commented Apr 5, 2020 at 1:11
  • 1
    @EvanMattson Hi Evan, thanks for the input. I'm new to both php and custom WP code and that has gone so far over my head. Would you be able to duplicate the code with the $wpdb->prepare() added if it isn't too hard? I think it'll be weeks before I get close to understanding that sort of thing.
    – pjk_ok
    Commented Apr 21, 2020 at 0:07
8

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.

6

I tried the solution of Onetrickpony above https://wordpress.stackexchange.com/a/5404/37612, which is great, but I found one issue there, which did not work for me, and I would make one small modification:

  1. if I searched for a string in the title of the taxonomy - it works great
  2. if the taxonomy has special characters e.g. with german "Umlauts" (ö,ä,ü) and one searches for oe, ae, ue insteda of using the special char - you need to add the search in the slug of the taxonomy - OR t.slug LIKE '%".get_search_query()."%'

  3. if you search for a combination of a search query and a taxonomy filter - this also works fine

  4. But the problem is, when you try to use only the taxonomy filter - the search hook append an empty string to the query if no text is searched for, and for that reason you get ALL posts in the result, instead of only those from the filtered taxonomy. A simple IF statement solves the problem. So the whole modified code would be this (works perfectly fine for me!)

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

function custom_search_join($join){
  global $wpdb;
  if (is_search()&& get_search_query())
    $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 custom_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 || !get_search_query()) 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','custom_search_where');
add_filter('posts_join', 'custom_search_join');
add_filter('posts_groupby', 'custom_search_groupby');
4

I found the answer from onetrickpony to be great but it treats any search as a single term and also won't deal with a search phrase enclosed with quotation marks. I modified his code (specifically, the atom_search_where function) a bit to deal with these two situations. Here is my modified version of his code:

// 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, $wp_query;
    if (is_search()) {
        $search_terms = get_query_var( 'search_terms' );

        $where .= " OR (";
        $i = 0;
        foreach ($search_terms as $search_term) {
            $i++;
            if ($i>1) $where .= " AND";     // --- make this OR if you prefer not requiring all search terms to match taxonomies
            $where .= " (t.name LIKE '%".$search_term."%')";
        }
        $where .= " 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');
3

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.

1
  • +1 For Search Everything plugin. It works as expected and returns more results than standard Wordpress search.
    – PNMG
    Commented Dec 2, 2010 at 22:00
2

I have the same problem going on with the WooCommerce cart plugin.. My search results are not including the custom taxonomy term, 'product_tag', because it not a standard post tag. I found a solution in this other StackOverflow thread about the matter:

https://stackoverflow.com/questions/13491828/how-to-amend-wordpress-search-so-it-queries-taxonomy-terms-and-category-terms

The code example by tkelly worked for me when replacing the term author in his example with product_tag as per our needs for the cart plugins.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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