• Resolved Andry

    (@blackstar1991)


    Thanks for plugin.

    I have a question – Is it possible to add some parameters for search request ? What I mean:

    I need that search page for reqests :

    https://test.com/?s=Compassion 
    https://test.com/?s=Compassion&post_type=profession
    https://test.com/?s=Compassion&post_type=post,page

    would give different results on a search page. Now the & symbol kills the query.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter Andry

    (@blackstar1991)

    I see this topic but don’t understand how it can help me with this task…

    Plugin Author Mikko Saari

    (@msaari)

    Yes, Relevanssi supports most of the standard WordPress query parameters. If adding the parameter breaks the query, something’s wrong on your site, and it’s probably not caused by Relevanssi. How exactly is the query killed? What happens?

    Thread Starter Andry

    (@blackstar1991)

    Oh, I think I found the answer to my question.

    function filter_relevanssi_results($hits) {
    if (!empty($_GET['post_type'])) {
    $allowed_types = explode(',', $_GET['post_type']);
    $filtered_hits = [];

    foreach ($hits[0] as $post) {
    if (in_array(get_post_type($post->ID), $allowed_types, true)) {
    $filtered_hits[] = $post;
    }
    }

    $hits[0] = $filtered_hits;
    }

    return $hits;
    }
    add_filter('relevanssi_hits_filter', 'filter_relevanssi_results');
    Plugin Author Mikko Saari

    (@msaari)

    You can do it like that, but it would be much better to set the post_type parameter before the search so you don’t have to filter afterwards. It would be much more efficient that way. Relevanssi reads thepost_type parameter from $_GET, so this should just work without any extra hoops. It sounds like there’s a more fundamental problem on your site, and tackling that would be more efficient than fixing it with a patch like this.

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.