Posted on

Taxonomy archive search

This is a Premium feature and only applies when you index taxonomy terms. This does not apply in free Relevanssi.

To include taxonomy pages (categories, tags, custom taxonomies) in the search, enable the option on the options page. There’s also the option to choose which taxonomies are included.

By default empty terms (not linked to any posts) are excluded from the index. If you want to modify that, see relevanssi_hide_empty_terms.

What is indexed?

Currently, the title of the taxonomy is indexed with the description field. Indexing the other fields isn’t currently possible. Taxonomy pages should be automatically updated in the index whenever they are edited.

Search results

In the search results template, avoid get_permalink(), as it doesn’t work. the_permalink() works and returns the correct permalink. You can use relevanssi_get_permalink(), if you don’t want to echo the permalink directly.

In the post object created from the taxonomy page, $post->post_title contains the name of the taxonomy, $post->post_content has the description, $post->link has the permalink and $post->post_type will contain the name of the taxonomy.

If you want to modify the post objects Relevanssi creates from the taxonomies, you can use the relevanssi_taxonomy_term_to_post filter.

49 comments Taxonomy archive search

  1. I’m using the free version. At the moment the search result shows the product title and the whole description. However i’d like it to show the product image (the 1st one would be fine) and the price. Is it possilbe?? Any help would be highly appreciated! Many thanks.

    1. That is just a question of setting up your search results template to show the product image and price. That should not be particularly tricky, given some HTML and PHP understanding.

      1. Thank you vvvvvvvvvery much for the reply. The guy who set up the website for me has gone and i’m on my own now:(. So should i do it in relevanssi/relevanssi.php file or somewhere else? Sorry for asking such a silly question but i’m not a technical person at all. Many thanks!

        1. No, you’re not supposed to touch the Relevanssi plugin files at all. The search results template is actually part of your theme, and can usually be found in the search.php file in your theme folder.

          If you’re not a technical person at all, I recommend hiring someone to do it for you, as it probably requires some PHP knowledge. Anybody with a decent level of WP theme knowledge can do it.

  2. i sell books and using your free plugin to search in my store. i really like it, only i am not succeeding with the taxonomy search. i add my “author” taxonomy to “Custom taxonomies to index” but when i search for an author name it doesnt show any result.

    reading here i understand its a premium option only, or am i doing something wrong?

    1. I’m guessing here, but I think the problem may be caused by a conflict, since “author” can also mean the author of the post. Try renaming your taxonomy to “book_author”. That might help.

      Finding posts by taxonomy is available in free version, it’s just finding taxonomy term pages is restricted to premium.

  3. Hi. In my site, Relevanssi is not indexing custom post type. They are marked like public and not excludad from search, even Relevanssi is not respecting this trought options page. ¿What can be hapennig? WP 3.5, Relevanssi Free last versión.
    Regards!

    1. There is usually a reason. Are you sure Relevanssi is not indexing it? Do the custom posts not appear in the database? It can be that Relevanssi is indexing them fine, but the problem is in searching.

  4. I have the premium version installed and configured to index my taxonomies, including a custom taxonomy. When searching, these taxonomy pages are not coming up in the results. Isn’t this supposed to be possible with the premium version?

    1. Yes, that is supposed to be possible. You’ve checked the correct taxonomies on the *lower* list of “taxonomies to index” on the Relevanssi settings page? You’re sure you’re getting your search results from Relevanssi?

  5. We’d like to be able to restrict the search results to a Page in the current section of the site where the user is. A section of the site is a Folder for a Page. Here’s an example of the hierarchy with Pages:

    site.com/bicycle/abcpage
    site.com/bicycle/defpage
    site.com/bicycle/tirestuff/ghipage

    site.com/moped/abcpage
    site.com/moped/defpage

    I would want the site to either search all bicycle pages or all moped Pages. In my research through your documentation I’ve found ways to use categories and tags for Posts, and it appears that you recommend Taxonomies for Pages. Should I use a custom field to create a (seemingly duplicate) Taxonomy on all of my pages within a certain section, or is there a simpler way to simply filter results to the “bicycle” or “moped” folders?

    Thanks so much for your help!

  6. This might sound strange, but I could not find an answer to this:
    Does this setting allow me to find posts from the selected taxonomies or should it show an actual link to the taxonomy-archive?

    1. It shows a link to the taxonomy term page. The other taxonomy setting (the one that is available in free Relevanssi) will find the posts that have the taxonomy term present.

  7. I’m using the free option. Have a custom post type with it’s own set of categories/taxonomy. The front end searches the posts fine. It does not bring up the category pages. The back end finds nothing in the posts but does work for the categories.

    Is this a free/premium issue or am I just not adjusting the settings properly?

  8. Hi i am using relevanssi premium which is a great plugin, I have custom taxonomies with different fields and description in it. I want to show relevant fields archive page when user types for particular word. Relevanssi gets it on the first result but read more button goes to home page for some reason any help would be appreciated

    1. Relevanssi used to include taxonomy terms even when the search was restricted by a post_type parameter, but that caused enough complaints that the default behaviour was changed. If you want taxonomy terms in the search, add the taxonomy name to the post_type parameter.

      1. I have such many custom taxonomies that i will loose flexibility when i write it hard coded. If i left it blank i’ve got all of them and can customize by WP Panel in plugin settings. Am i right ?

        1. Yes, no “post_type” restriction in the search means all taxonomies are included. If the “post_type” restriction never changes, it’s best done in Relevanssi indexing settings, but you can also use “relevanssi_hits_filter” to weed out posts.

          1. Yeah, I just discover this becouse of my custom query for search wich is required for my AJAX implementation. So i’ve done it via relevanssi_do_query() function. Thanks anyway 😉

  9. Dear Mikko,

    I’m currently developing my website, and have installed your plug-in.

    There are two different pages A and B on my website where I’d like to insert Relevanssi search forms: form As for A and form Bs for B.

    Both forms need to search through pages with a specific taxonomy only, that is:

    As needs to search through pages with the tag ‘ads’ only.
    Bs needs to search through pages with the tag ‘statistics’ only.

    I think this might not be so complicated, but I don’t know how to do it.
    Do I need to insert some code in the html form code, in the search php code or in both?

    Any help would be greatly appreciated.

    Best,

    Luca

  10. Hi,

    I’ve got premium and it works great. I get all the results as expected and have applied weights to various post types.

    However, when I applied the highest weight to a custom taxonomy to get it to be returned first at the top of the page, it doesn’t work.

    Do weights work for taxonomies?

    How can I get a taxonomy to be displayed at the top of each search returned.

    1. Weights do work for taxonomies, but not for taxonomy terms. Terms are not affected by the weights, the weights apply to posts where the search query matches a taxonomy term.

      To get the taxonomy terms on top of the search, use a post type based weight:

      add_filter('relevanssi_match', 'extra_term_weight');
      function extra_term_weight($match) {
      	$post_type = relevanssi_get_post_type($match->doc);
      	if ("XXX" == $post_type) {
      		$match->weight = $match->weight * 1000;
      	}
      	return $match;
      }

      Replace “XXX” with the name of the taxonomy. That should lift the taxonomy terms on top of the results.

      1. Hi Mikko,

        Thanks for your response. I think I asked the wrong question.
        What I actually want is the taxonomy term pages. (mysite/taxonomy/myt_taxonomy_term)
        I want the taxonomy term pages to come to the top of the search result.
        Can this be done?

  11. Hi Mikko,

    Thanks for the response.
    I know it’s been a very long while since you answered the above for me.

    I have tried the code above. I added it into my child-theme functions.php and changed the “XXX” to the name of the taxonomy.

    But the taxonomy terms do not come to the top of the page. They still just appear randomly in the search results.

    1. Eriq, I would suggest you debug it. For example, you could add var_dump( $post_type ); after the relevanssi_get_post_type() line to see what the post type is for those terms to make sure you’re using the right one. That’d be a good way to start.

  12. Hey everybody,

    I just installed your Plugin for the following task:

    I want the search to only search for post_tag. I dont care for the titel of a post neither for the content. I just want results for the custom post_tags.

    The plan:
    I want to add custom “winner numbers” to a post as a post_tag, like 12345 and I want the chosen post to apper, when the number is entered in the search.

    Right now the porblem is, that when the number apperas on the post itself, like f.e. as a zip code, the result is shown.

    Can you help me only show the post_tags?

    Best regards,
    iRomeK

      1. Thank you very much. I am not that Pro in WordPress, so is there any construction or how to video?

        Thank you very much.

        Beste regards,
        iRomeK

        1. Just add those two lines to your theme functions.php file. If that sounds difficult, I’m sure there are instructions available for editing the theme functions.php file

          1. Hey Mikko,

            I added both functions to the functions.php of my child theme.

            Reindex gives me the following output:
            Wiping out the index… Done.
            Counting posts… 2 posts found.
            Indexed 0 posts (total 0), processed 2 / 2.
            Indexing complete. 2 posts excluded.

            But when I search, I can still search the content? Any Ideas?

            Tank you very much.

          2. Looks like your search is not using Relevanssi in the first place. Try the admin search (Dashboard > Admin search) and see if that works better.

  13. Wow, I saw your post about BeTheme. Now I added your requested code and the search works fine!! Awesome!!

    Just one last (little) thing. The Exerpt is not sown in the result. There is just “(mehr…)” instead of the exerpt I wrote on the blog.

    Awesome support so far! Thank you!!

    1. So you want the search results to show the excerpt you’ve written, and not the one Relevanssi generated? Make sure excerpts are disabled in the Relevanssi settings. If it still doesn’t work, it’s a theme problem, and you need to contact BeTheme support.

  14. Hi. I have the premium version. Trying to figure out a custom search form that will search *just* the current taxonomy instead of the entire site contents (I’ll have that capability elsewhere). I have assigned a custom taxonomy to the Media area, and have assigned images to different taxonomy terms. So on the taxonomy page, I’d like to be able to search by whatever taxonomy the user is currently on – for instance, “library” within the mediacat taxonomy. How would I do that?

    Thanks in advance,
    ~Laura

    1. Laura, Premium support is here.

      To filter the search by a taxonomy term, you need to add an extra parameter to the search – set the mediacat parameter to library for example. Your search form needs an extra bit of code that will snoop out the term for the current active page and then add that as a parameter to the search form.

  15. I have this installed and working on the front-end. But my main point in using it was so taxonomies and tags could be searched from the admin. I have “use for admin” search checked. What else could I be doing wrong?

    1. Laura, it may be you’re not doing anything wrong. In some cases the backend search just doesn’t work with Relevanssi: for example the Pages search simply doesn’t use Relevanssi for reason I don’t really know. Product search with WooCommerce is another one, where some people say Relevanssi is not being used.

      To get a Relevanssi search in the admin, the best solution is to use the Relevanssi admin search (Dashboard > Admin search). That’s guaranteed to be Relevanssi.

      Relevanssi is mostly a front-end search, and the support for admin search is just a bonus, and not a big priority for us.

  16. Hi Mikko – I love that Relevanssi displays taxonomy pages in the search results! How can I get the tag_id for taxonomy pages as a post object? Post_ID returns -1 for all taxonomy pages, but I’d guess there is a way to extend Relevanssi to capture it.

    1. William, the term ID is in $post->term_id for the taxonomy term posts, it’s automatically added there (the taxonomy, should you need it, is in $post->taxonomy).

  17. If it’s all right with Mikko, here is the code I’m using to pull an image stored in Advanced Custom Fields on category and archive pages (which cannot have featured images in WordPress). The first bit is to pull the featured image for posts, and the second bit is for tags and archives.

    taxonomy = ‘category’ OR $post->taxonomy = ‘post_tag’) {
    // Display ACF image from taxonomy pages (my field is named archive_featured_image, returns image ID)
    $term_id = $post->term_id ;
    $taxonomy = $post->taxonomy;
    $taxonomy_ID = $taxonomy.”_”.$term_id;
    //ACF looks for fields on taxonomy pages using get_field(‘fieldname’,category_101), so we concatenate the taxonomy with an underscore and then the term_id. This works for tag archives as well as category archives
    $image = get_field(‘archive_featured_image’,$taxonomy_ID);
    $size = ‘thumbnail’; // (thumbnail, medium, large, full or custom size)
    if( $image ) {
    echo wp_get_attachment_image( $image, $size );
    }
    }
    ?>

    Thanks again Mikko. Relevanssi rocks!

Leave a Reply

Are you a Relevanssi Premium customer looking for support? Please use the Premium support form.

Your email address will not be published. Required fields are marked *