• Hi,

    I’m having trouble getting the correct search results from Relevanssi. I’m creating a query from four form fields set by the user. The generated tax_query should flow through Relevanssi and return the correct results.

    I haven’t fully grokked how Relevanssi works, but I thought maybe somebody could offer a suggestion on where to continue debugging, based on this situation:

    When I look at what’s happening in relevanssi_do_query(), I can see that my tax query variables are coming through ok.

    When I trace out the following at the end of relevanssi_do_query()

    print_r($query->query_vars['tax_query']);

    I get this

    http://pastebin.com/1Gmu5rTq

    These are the correct params (in my test case I’m only setting one of four arguments: I want to return all posts marked with an ‘English subtitles’ taxonomy term).

    When I run a sql query directly on the db, I get 267 results where the English subtitles taxonomy term is set.

    However, when I trace out $query->post_count at the end of relevanssi_do_query() it only shows 36. Strange, I would think it should be 267.

    Futhermore, when I trace out $query->tax_query at then end of relevanssi_do_query(), I get this

    http://pastebin.com/YnA7SzgF

    Why would $query->tax_queries[“queries”] only show two of the four taxonomy query variables?

    Thanks for any thoughts. I’m still learning WordPress so pardon the question if I’m missing something obvious to experienced users.

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

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

    (@msaari)

    What’s your search term? That matters as well, Relevanssi requires you to include a search term. You can’t use Relevanssi to just fetch things by tax_query (also, that’s a huge waste of resources to do so, because you’re not using Relevanssi for anything).

    Thread Starter DanielMcQ

    (@danielmcq)

    Hi Mikko,

    Thanks for your response. In this case, I wasn’t using a search term.
    The search form has a text field for search terms and then four controls corresponding to taxonomies : Video types (pulldown), video topics (pulldown), English subtitles (checkbox), Educator Resources (checkbox).

    In my test case I’m assuming the user wants to see all videos with english subtitles, so I’m selecting the “English subtitles” checkbox and submitting.

    The current code follows the example for creating multiple-taxonomy searches you posted here.

    I have a similar function that collects the four query args from the form and pushes them into the tax_query property.

    http://pastebin.com/aaLVDdBP

    You wrote “You can’t use Relevanssi to just fetch things by tax_query (also, that’s a huge waste of resources to do so, because you’re not using Relevanssi for anything).” So are you saying that if we’re trying to use these four fields to allow the user to limit results by different taxonomies, we shouldn’t use Relevanssi?

    Thanks for your quick response on the first post. Much appreciate your help.

    Plugin Author Mikko Saari

    (@msaari)

    If you don’t have a search term, you don’t have a search, and Relevanssi can’t help you.

    WordPress has good tools for fetching posts by taxonomy, and using something else just adds unnecessary overhead without providing anything useful.

    There are solutions to make searches work without a search term, but that involves writing code that fetches the required posts manually, using the aforementioned WordPress tools, when there is no search term present.

    Thread Starter DanielMcQ

    (@danielmcq)

    Hi Mikko,

    Ok, thanks for clarifying. So if we have a search form that has a search term field as well as fields for taxonomies that limit the search, if a user chooses to search just by a taxonomy, we need to use the standard WP search. We can only use Relevanssi if the “s” arg isn’t empty.

    I imagine I can do this using the method you’ve described in previous posts:

    remove_filter(‘the_posts’, ‘relevanssi_query’);
    remove_filter(‘posts_request’, ‘relevanssi_prevent_default_request’,9);
    remove_filter(‘query_vars’, ‘relevanssi_query_vars’);

    I guess the trick it to know where to check for s==”” and then run those remove_filter methods.

    Thread Starter DanielMcQ

    (@danielmcq)

    One follow up question would be, which array does Relevanssi use for taxonomy queries if “s” does have a value?

    $query->tax_queries[“queries”]
    or
    $query->query_vars[‘tax_query’]

    Thanks.

    Thread Starter DanielMcQ

    (@danielmcq)

    I’m posting what I found here in case somebody else runs into this problem:

    Relevanssi will sanitize your taxonomy title as part of the sql generation that occurs in the relevanssi_search() method in relevanssi-premium/lib/search.php.

    If you have a taxonomy name like “English Subtitles”, it will get sanitized to “english-subtitles” and then Relevanssi won’t be able to find the related terms for that taxonomy.

    In my case, the sql query to find the terms for this taxonomy was constructed as

    SELECT tt.term_taxonomy_id
    FROM wp_term_taxonomy AS tt
    LEFT JOIN wp_terms AS t ON (tt.term_id=t.term_id)
    WHERE tt.taxonomy = 'english-subtitles' AND t.term_id IN (267)

    when it should have been

    SELECT tt.term_taxonomy_id
    FROM wp_term_taxonomy AS tt
    LEFT JOIN wp_terms AS t ON (tt.term_id=t.term_id)
    WHERE tt.taxonomy = 'English Subtitles' AND t.term_id IN (267)

    I’m relatively new to WordPress and have inherited an existing site, so I don’t know if there are standards for naming taxonomies. But it seems here that if you name your taxonomy in a way that will cause it to be different after sanitization, the query won’t work.

    Plugin Author Mikko Saari

    (@msaari)

    Relevanssi uses either $wp_query->tax_query or $wp_query->query_vars[‘tax_query’]; the first is what WP creates, the second is what users create.

    That’s a bug you’ve found, by the way, that sanitize_title() there is wrong. I’ll have to fix it in the next version. Thanks!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Advice on taxonomy problem’ is closed to new replies.