• Hello. I’m trying to modify my search results to only search within the currently tagged taxonomy. I created a Location taxonomy on a site with Country/State/Region/City/etc… For example if someone is on a page that’s tagged with a City I only want the search to search within the City that is tagged.

    Is this possible with Relevanssi?

    https://wordpress.org/plugins/relevanssi/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Mikko Saari

    (@msaari)

    Yes. All you need to do is to add a taxonomy parameter to the search form, depending on which page you are.

    Put something like this in your search form:

    <?php if (you're on a tagged page) : ?>
    <input type="hidden" name="Location" value="<?php // get the taxonomy value and echo it here ?> />
    <?php endif; ?>

    Something like that, just fill in the details. That used to work just like that, but using taxonomies directly as query parameters was deprecated in 3.1 in favour of tax_query structure. So, this may or may not work directly, but if it doesn’t, you need to add another filter that reads in the Location query parameter and constructs a tax_query out of it (see WP_Query in Codex).

    Thread Starter caleb_m

    (@caleb_m)

    That’s what I’ve been trying. I got it to work manually, but can’t get it to echo out the taxonomy in the value=””

    Thread Starter caleb_m

    (@caleb_m)

    I managed to get to this point.

    <?php
    $taxonomy = 'location';
    $terms = wp_get_post_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
       if( $terms ) {
       echo $term->term_id;
       }
    ?>

    The problem I have now is that it outputs the first id in the list and not the last. So it echos the id for United States not Millbrae

    Terms hierarchy –
    United States
    California
    Peninsula
    Millbrae

    The other problem I have is if I try add that to the value in the search input I get location=%0D%0A%09&s=pizza instead of location=11742&s=pizza

    Plugin Author Mikko Saari

    (@msaari)

    %0D%0A%09 is a space, a return and a linefeed.

    You echo $term->term_id – where is that $term defined? Above that the variable is called $terms.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Limit search results to the current tagged taxonomy on the page’ is closed to new replies.